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

  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!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 21 (21 by maintainers)

Commits related to this issue

Most upvoted comments

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.

@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.

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 in load or handle. Doing it in SvelteKit would make it much easier to migrate to another host should you ever have a desire to do that