-
-
Notifications
You must be signed in to change notification settings - Fork 463
RFC: Redirects/rewrites/headers support #587
Comments
Update: sorry for the delay, I have been busy helping stabilize this component with end-to-end tests and other bug fixes. I will start working on the matcher next (probably this weekend or so) |
Have created the first PR (matcher + redirects): #627. @danielcondemarin could you please review it when you have time? |
Have published the redirect support in the latest alpha: https://www.npmjs.com/package/@sls-next/serverless-component/v/1.17.1-alpha.1. If you like, please try it out and let us know if you have any issues (Caveat: it doesn't support redirects from API routes yet, will be adding in a future PR). |
Thank you for doing that! I just tried it with my codebase and got Here is my next config: const withImages = require('next-images');
module.exports = withImages({
async rewrites() {
return [
{
source: '/sitemap.xml',
destination: '/api/sitemap',
},
{
source: '/feed',
destination: '/api/feed?type=rss',
},
{
source: '/feed.rss',
destination: '/api/feed?type=rss',
},
{
source: '/feed.atom',
destination: '/api/feed?type=atom',
},
];
},
async redirects() {
return [
// Article redirects
{
source: '/articles/:slug',
destination: '/community/articles/:slug',
permanent: true,
},
];
},
}); You mentioned no redirects for APIs but I am assuming that meant redirecting from API not to API, I could be wrong though I'll look. |
@panbanda yes right now having an For that |
Yep you were right. Looks great redirects are working well! Should we start another issue for rewrites to /api/* or doing that here as well? |
@panbanda The latest alpha should work with API paths as a redirect source too. Please try out and let us know if there are issues. |
Looks good but I am noticing something in the
I also tried deploying with the rewrites and neither worked. I haven't checked to see if rewrites were implemented yet. async rewrites() {
return [
{
source: '/sitemap.xml',
destination: '/api/sitemap',
},
{
source: '/feed',
destination: '/api/feed?type=rss',
},
{
source: '/feed.rss',
destination: '/api/feed?type=rss',
},
{
source: '/feed.atom',
destination: '/api/feed?type=atom',
},
];
}, |
@panbanda yeah rewrites are not implemented yet. Do you mind creating a new issue for the redirect bug, just so when it's fixed it's easier to reference that as its own issue. I'll take a look later this week, but curious what does the Lambda respond with? Does it still respond with a 308 (redirect), just that the location header is wrong so it 404s? Edit: found the problem, it's because of the query string at the end, |
The latest alpha should have the fixes for redirects, and also adds rewrites support (except for external URLs are not implemented yet): https://github.com/serverless-nextjs/serverless-next.js/releases/tag/%40sls-next%2Fserverless-component%401.18.0-alpha.2. Please try it out and open an issue in case of any bugs. |
Good job it try it today |
This great, but not working for me? I'm on module.exports = withSourceMaps(withImages(withCSS(withBundleAnalyzer({
target: 'serverless',
cssLoaderOptions: {
url: false
},
env: {},
webpack: (config, options) => {
// some configs
},
async redirects() {
return [
{
source: '/dashboard',
destination: '/otherplace',
permanent: true
}
]
}
} Am I missing a config or setting? |
@justingshreve the handler code should parse the redirects from routes-manifest.json which should be in the Lambda package as well. Can you please share the routes-manifest.json and index.js (this is the handler code that does it) in the generated .serverless-nextjs/default-handler directory. It would help to debug what's wrong. |
Just wondering, having trouble grokking it reading this thread, did the new header functionality get into the alpha? |
@tyler-ground headers are not yet there as there is some implementation challenge due to the But we can first implement custom headers for any response returned in the origin request handler (SSR pages, SSR data requests, redirects etc). For future reference you can check the features in https://github.com/serverless-nextjs/serverless-next.js#features or changelogs in https://github.com/serverless-nextjs/serverless-next.js/releases (I know it's a little hard to parse as there are many dependent packages whose changelogs don't get rolled up into the main package's, we are trying to see how we can make this cleaner). |
Thanks! I saw the headers ticket closed and this was talking about features in alpha-3. The readme has "Custom Headers" listed as a feature so I assumed it was probably done but it wasn't working for me. :) |
@tyler-ground yeah, it is listed but the checkbox is not ticked yet, so it is not ready. I'll update the readme to make it more clear. |
OH! haha, I completely didn't notice that. Thanks :) |
@justingshreve Did redirects start working for you? I'm on 1.18.0-alpha.4 but they don't work (I get a 404 page instead). However, I swear these were working in an earlier build but I've gone back and had no success. They do work locally. Edit: I wonder if this is perhaps another case of the wrong lib getting into the builds. |
@tyler-ground, it should not be, just deployed from 1.18.0-alpha.4 and it works fine. I did fix lerna publish (back in one of the 1.17 alphas) to publish with exact versions of dependent internal packages, so this should no longer be an issue: https://github.com/serverless-nextjs/serverless-next.js/blob/master/package.json#L22-L24 To be sure, I checked 1.18.0-alpha.4's package.json and it looks good (all @sls-next versions are pinned):
I also checked Are you able to share the build output files in Also, do be sure that the Lambda being called has already been deployed to your closest CloudFront edge location (in case perhaps you were switching between 1.17 (which has no custom redirects) and newer 1.18 alphas, etc.) - you can verify by checking an SSR page and making sure the build version in the page source matches the one you recently deployed. Usually it's within a couple of minutes, but a few times it took a long time for me, like 15-30 minutes. Thanks. |
Headers support has been released in the latest alpha (https://github.com/serverless-nextjs/serverless-next.js/releases/tag/%40sls-next%2Fserverless-component%401.18.0-alpha.5) - with a caveat due to our use of S3 origin / origin response handlers and rewriting the URI to the S3 key. E.g if you want to add a header to any path mapping to an S3 object, such as an SSG page, you must specify the source as the S3 key (without prefix such as We will work on addressing this caveat in the future. Please try out and let us know of any bugs by opening a new issue. PS: a couple of people have mentioned that these new features are not working for them. I am unsure why that's the case, as I did check the latest 1.18 alpha versions and confirmed they are using pinned internal dependencies and have the redirect/rewrite/header logic in the handler code. If it's not working, I suggest the following:
Thanks! |
@dphang Thanks, my deployment systems are setup such that I don't retain artifacts but when I'm home later this weekend I'll modify them to store the files you requested in S3 and include. I am 100% sure as I waited 5 minutes between each deploy (I spent a few hours messing around with this). 👍 |
Closing this issue for now as I believe all of the functionality is implemented, except with the caveats are mentioned in the README but it is due to how the component is architected (e.g due to having both origin request/response handlers, and some cache behaviors do not have handlers to do rewrite/redirect/headers functionality). Will revisit any improvements in the future. |
@dphang should this now work with external URL's, this is the feature we would really appreciate for our go live with next js, is there anything you would need help with doing that to get it out ASAP |
Just starting this thread about custom redirects/rewrites/headers support. It seems to be the most requested feature in Next.js 9.5 as of now, so I think it makes sense to support this next.
Next.js docs:
Redirects: https://nextjs.org/docs/api-reference/next.config.js/redirects
Rewrites: https://nextjs.org/docs/api-reference/next.config.js/rewrites
Headers: https://nextjs.org/docs/api-reference/next.config.js/headers
I am grouping these three features together they are similar and were added at the same time:
routes-manifest.json
after Next.js build, so it's already accessible in the handler code. Note, Next.js does have the regexes stored here as well.Note that there is existing code that I wrote to handle trailing slash redirects (which is not using the default-added redirect in
routes-manifest.json
right now for perf reasons)Since we are now using Rollup.js to bundle the handlers, we can write separate files for better maintainability. I suggest the following steps:
res
headers in the page code itself.routes-manifest.json
for theredirects
/rewrites
/headers
list.I imagine the tricky part would be the path matching.
I'm already working on the e2e tests, so if someone wants to start on this or has any thoughts on the design, feel free to add your thoughts here. If not, I will work on this sometime later this month.
@danielcondemarin FYI.
The text was updated successfully, but these errors were encountered: