nitrojs / nitrojs/nitro

Wildcard redirects do not work with Netlify because Netlify routeRules do not support forced redirects with !

Open
#1,415 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug nuxt preset:netlify v2
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

   Forwarded from downstream issue:


Environment

  • Operating System: Darwin
  • Node Version: v16.20.0
  • Nuxt Version: 3.6.2
  • Nitro Version: 2.5.2
  • Package Manager: npm@8.19.4
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

N/A

Describe the bug

I am attempting to add wildcard redirects to our site, such as

export default defineNuxtConfig({
  routeRules: {
    "/downloads/firmware/*": {
        redirect: {
          to: "/downloads/firmware",
          statusCode: 301,
        },
    },
  },
});

This works locally just fine, but when deployed to Netlify it doesn't. I initially assumed this was a Netlify issue, but having spoken to a netlify engineer they said the limitation is that the statuscode in Nuxt requires a number not a string, meaning we can't add the required ! parameter. I did try statusCode:"301 !" but this didn't work. See below response from Luke Lawson at Netlify, who explains it better than I can:

Nuxt itself is designed to use Netlify's redirects feature.

If you download the deploy, you will see that it does contain a file named _redirects and that this file includes the redirect you mention in the ticket opening:

/downloads/firmware/* => '/downloads/firmware'

Line 32 of the _redirects file from the deploy has that same redirect using Netlify's syntax:

/downloads/firmware/* /downloads/firmware 301

Again, the Nuxt framework is generating that file - not Netlify. Nuxt is adapting itself to Netlify. Netlify is not creating that file.

So, the redirect does exist but it still clearly isn't working. The explanation is that the rule above isn't triggering because of the default 404 page behavior:

https://docs.netlify.com/routing/redirects/redirect-options/#custom-404-page-handling

Quoting that documentation:

You can set up a custom 404 page for all paths that don’t resolve to a static file. This doesn’t require any redirect rules. If you add a 404.html page to your site, it will be picked up and displayed automatically for any failed paths.

You will also notice that the downloaded deploy contains a 404.html file. That file is superseding the redirect rule above for the path /downloads/firmware/test. All 404s are being sent the contents of 404.html. As /downloads/firmware/test is a path that will 404, the redirect rule isn't triggering.

The way to fix that using the _redirects file is to "force" the 301 rule to apply like so:

/downloads/firmware/* /downloads/firmware 301!

However, I don't see any way in the Nuxt to add the ! character

Additional context

No response

Logs

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how routeRules are converted into the Netlify _redirects file, focusing on wildcard redirects and numeric statusCode handling. Done means a forced redirect can be represented and the generated rule includes Netlify's 301! syntax so it takes precedence over the 404.html behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
nuxtjs, typescript
Domain
cloud, devops
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.