Closed joanniclaborde closed 9 years ago
Neat :+1:
Rebased, we're going to give this a go on a Jason Aldean splash page that's going to live at www.jasonaldean.com. Need to redirect all the original Clique Tools routes to some temporary subdomain till we go live with the rebuild.
We didn't end up pushing that live, but regex worked out great. We should merge this for 0.3.0.
This is a breaking change, it needs to go in 1.0.0.
Rebased, delete your local branch!
Note: this is merged into https://github.com/solidusjs/solidus/tree/1.0.0
Looks like I can't match things in a query string, is that by design since routing is otherwise path-based? In my case I'm trying to merge a query string from a redirect with another at the target URL.
http://expressjs.com/3x/api.html#app.VERB
Query strings are not considered when peforming these matches, for example "GET /" would match the following route, as would "GET /?name=tobi".
It looks like the Express router is too basic for your needs :(
What about actually executing some code to get the value of to
? In my case I've got a series of routes that spell out the names of months, i.e. january
, october
. But in the destination I need them to be their numbered equivalents. to
could accept a function with any variables or capture groups as arguments that returns the destination path. That would provide a ton of flexibility in situations like this.
Something like this (I'm about to commit)? The dev can do his own interpolation with params
, or let Solidus expand the string returned by the function. Or do both:
{
from: "/redirect8/{dynamic}/{route}",
to: function(params) {
return "/new/{route}/" + params.dynamic.toUpperCase();
}
}
Perfect!
Fixed, tell me if you need it in the 108-dynamic-redirects-longer-timeout or 1.0.0 branch.
108-dynamic-redirects-longer-timeout would be great, thanks
Done.
Works great! What are your thoughts on resource fetching in a redirect? I've got a scenario where a content item no longer has a dedicated page, but rather appears in a parent page as an anchor link. Problem is in order to get the necessary data for routing to the parent I need to make a request.
This would likely also solve any scenarios in Solidus across the board where a given resource's parameters similarly requires a dynamic value from a sibling resource. This is often due to constraints in an API that are sometimes addressed with field expansion. This has been avoided in the past with XHR hacks and in the worst cases, meta redirects. These target functions seem like a much better place for this code. They've already taken us beyond simple redirects and effectively provided a place for more complex routing rules than the filesystem allows.
Also currently I need to restart the server to pickup any changes I make to redirects. Can this be added to the file watcher, refreshing the logic whenever the file is changed?
Anyway to support named functions with additional arguments? Doesn't seem to work.. no error handling either, the redirects just break. Was trying to DRY up some logic, like:
function normalize( params, targetPath ){
// do stuff to params.param
...
return targetPath + result
}
module.exports = {
'from': '/path/{param}',
'to': normalize(params, '/new/')
}, {
'from': '/path2/{param}',
'to': normalize(params, '/new2/elsewhere/')
}
For now I'm just calling my function from within the anonymous callback.
Your normalize
function needs to return a function, that will get executed at runtime when the redirection happens.
Re: resource fetching before redirecting.
Doesn't #125 address this problem?
Re: reloading redirects.js
The file is already watched, but the existing redirects were not removed before the file was reloaded. https://github.com/solidusjs/solidus/commit/4e46807741d3aaf5da05e42dce3172628472ef44 fixes this.
You mean like this?
return function(){
return path
? path + normalized
: normalized;
}
}
Doesn't seem to get called.
Oops I was on the wrong route!
Same issue though.
function normalize( params, targetPath ){
// do stuff to params.param
...
return targetPath + result
}
module.exports = {
'from': '/path/{param}',
'to': function(params) {return normalize(params, '/new/')}
}, {
'from': '/path2/{param}',
'to': function(params) {return normalize(params, '/new2/elsewhere/')}
}
Yeah that's what I'm doing now, was trying to avoid that =)
sooo.. Let's merge this and bump to 1.0 :+1:
Fixes #108
The
redirects.json
file is renamed intoredirects.js
, and it is now a module which must export the array of redirections. Redirections can have dynamic parts, or be regular expressions. Self-explanatory example: