Netlify Adapter: Custom redirects not working
See original GitHub issueDescribe 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!
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:21 (21 by maintainers)
Top Results From Across the Web
Custom domain and Redirect not working - Support
I am having a problem. My custom domain and redirect are not working despite configuring them. My site doesn't show.
Read more >Custom headers on HTTP 200 redirects - Netlify Support Forums
I'm running SvelteKit with the Netlify adapter. It generates the following _redirects file: * /.netlify/functions/render 200.
Read more >Video Content Shows on Netlify Site, Fails on Domain Redirect
I replaced the video files themselves with the same results, plays fine on netlify site deploy, doesn't work on domain redirect site.
Read more >_redirects not working - new to netlify - Netlify Support Forums
This will redirect any path at the netlify domain to the primary domain if someone tries it. It will also tell google to...
Read more >[Support Guide] Making redirects work for you
If you think you've configured your redirects correctly but they're still not working, take a look at your site's deploy log.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.