kit: Netlify Adapter: Custom redirects not working
Describe the bug
Custom Netlify redirects don’t work. Neither using netlify.toml
nor static/_redirects
.
To Reproduce
- Setup a minimal SvelteKit app using the Netlify adapter
- Specify a redirection rule, e.g. put
/old /new
instatic/_redirects
- Deploy the build output on Netlify (or test it locally using Netlify Dev)
- Navigate to https://YOUR-TEST-PAGE.example/old
- Now you’ll get a Svelte 404 error
Expected behavior
- Instead of the 404, my website should actually redirect to https://YOUR-TEST-PAGE.example/new
- Document that redirections in
netlify.toml
aren’t supported (for now?) because the adapter itself defines a “catch all” in_redirects
making all innetlify.toml
unusable
Information about your SvelteKit Installation:
- @sveltejs/kit: next => 1.0.0-next.64
- @sveltejs/adapter-netlify => 1.0.0-next.4
- svelte: ^3.29.0 => 3.36.0
- vite: ^2.1.0 => 2.1.4
Severity
For me personally? High, as I’m planning to replace an old website with SvelteKit and have to gracefully deal with old URLs. At least the _redirects
definitions should work.
Additional context
- I already noticed that the adapter is simply overwriting my own
_redirects
file with its own content becausewriteFileSync()
is used. - When investigating the issue locally I tried to change it to
appendFileSync()
(which makes more sense in my opinion). - Then however I’m getting the following:
Must use import to load ES Module: MY-PROJECT/functions/render/index.js require() of ES modules is not supported. require() of MY-PROJECT/functions/render/index.js from /npm/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from MY-PROJECT/package.json.
- Renaming
index.js
toindex.cjs
as suggested leads to another error.
Here I had to stop because I don’t know what that error above exactly means. I hope my report is helping though. Nice project btw!
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 21 (21 by maintainers)
Commits related to this issue
- fix: allow custom redirects for Netlify Adapter https://github.com/sveltejs/kit/issues/930 — committed to kvn-shn/svelte-kit by kvn-s 3 years ago
- fix: allow custom redirects for Netlify Adapter https://github.com/sveltejs/kit/issues/930 — committed to kvn-shn/svelte-kit by kvn-s 3 years ago
- fix: allow custom redirects for Netlify Adapter https://github.com/sveltejs/kit/issues/930 — committed to kvn-shn/svelte-kit by kvn-s 3 years ago
- fix: allow custom redirects for Netlify Adapter https://github.com/sveltejs/kit/issues/930 — committed to kvn-shn/svelte-kit by kvn-s 3 years ago
- Allow custom redirects for Netlify Adapter (#1484) https://github.com/sveltejs/kit/issues/930 Co-authored-by: Kevin Schneider <kevin.schneider@iteratec.de> — committed to sveltejs/kit by kvn-shn 3 years ago
- Allow custom redirects for Netlify Adapter (#1484) https://github.com/sveltejs/kit/issues/930 Co-authored-by: Kevin Schneider <kevin.schneider@iteratec.de> — committed to andyburke-forks/kit by kvn-shn 3 years ago
Thought about it even more. 😉 We should try to make it even smarter:
[[redirects]]
in the TOML:appendFileSync(publish + '/_redirects', '\n\n/* /.netlify/functions/render 200')
thing. It should automatically create the file if it doesn’t already exist. Else,\n
ensures that it’s added in a proper new line.Additionally we should point out in the readme that no (other) custom “catch all” redirects are allowed, neither in
netlify.toml
nor in_redirects
.@benmccann true, but it will require several lines of code to do what you could do with one line in
_redirects
or with a simplenetlify.toml
config. It’s probably faster too. For the same reasons, on my own server I do redirects in nginx config instead of in application code.I guess it comes down to personal preference, but either way, I think in principle the adapter shouldn’t break Netlify’s built-in functionality unless it really has to.
I will need this when I use SvelteKit in production so I’d be happy to take a look – but I probably won’t have time in the near future, so if anyone else wants to get to it before me, go ahead! 😄
Regarding doing it in SvelteKit so that migrating to another host in future is easier – I wonder if a good approach would be to add the redirects to SvelteKit’s config, and then the individual Adapters (Netlify, Vercel, etc) could output them in the appropriate format for each host? Of course that’s more work to implement, but it would give greater portability.
@benmccann but that wouldn’t work for a static site – unless I’ve misunderstood what you’re suggesting?
Surely the simple solution is for SvelteKit to append (or prepend) to
_redirects
rather than overwriting it. Would that be difficult?Is there a reason you even need to define custom redirects in
netlify.toml
? You can do it in SvelteKit itself either inload
orhandle
. Doing it in SvelteKit would make it much easier to migrate to another host should you ever have a desire to do that