question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Netlify Adapter: Custom redirects not working

See original GitHub issue

Describe the bug Custom Netlify redirects don’t work. Neither using netlify.toml nor static/_redirects.

To Reproduce

  1. Setup a minimal SvelteKit app using the Netlify adapter
  2. Specify a redirection rule, e.g. put /old /new in static/_redirects
  3. Deploy the build output on Netlify (or test it locally using Netlify Dev)
  4. Navigate to https://YOUR-TEST-PAGE.example/old
  5. Now you’ll get a Svelte 404 error

Expected behavior

Information about your SvelteKit Installation:

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 because writeFileSync() 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 to index.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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:21 (21 by maintainers)

github_iconTop GitHub Comments

2reactions
kvn-shncommented, Apr 10, 2021

Thought about it even more. 😉 We should try to make it even smarter:

  1. Check if the adapter-specific redirection is in the TOML:
    • Yes: nothing to do here anymore, proceed normally.
    • No: continue.
  2. Check if there are any other [[redirects]] in the TOML:
    • Yes: fail with an error, e.g. “Netlify adapter’s redirection rule is missing. Please add […] at the end of your netlify.toml”
    • No: continue.
  3. Do the 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.

1reaction
caesarcommented, May 18, 2021

@benmccann true, but it will require several lines of code to do what you could do with one line in _redirects or with a simple netlify.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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found