GithubHelp home page GithubHelp logo

Comments (10)

joaogarin avatar joaogarin commented on May 18, 2024 16

This is now fully supported (not experimental) in Nextjs. is this something that will be eventually added / supported? I would like to consider moving an app to netlify but currently it does use nextjs redirects and rewrites and so would be tricky not having those work anymore if I decide to move it =) thank you! happy netlify client ;)

from next-runtime.

FinnWoelm avatar FinnWoelm commented on May 18, 2024 2

Hey @scopsy,

Thank you for the kind words and for filing this issue to make next-on-netlify better 🙂

I had a closer look at the NextJS experimental syntax for redirects and rewrites as well as the limitations that apply to redirects on Netlify. There are a couple issues:

Netlify's redirect syntax is a lot more narrow than the one used by NextJS

Netlify only supports redirects with plain text, with parameter placeholders (/:id/:slug/:abc/...), and/or with match-all splats (/some/path/*). For example, the last rewrite you shared would not work on Netlify:

`/:locale((?!${noRedirectBasePaths.join('|')})[^/]+)(.*)`

There is no direct way to express that in a Netlify-compatible way. With Netlify, you would probably define it as a catch-all /* rewrite and position it after all other rewrites and redirects. Because Netlify only uses the first redirect/rewrite it finds and because of shadowing, Netlify would not apply the rewrite in any case where the user requests a page that actually exists.

So it's possible to express the same rewrite in Netlify, but I can't think of a way to automatically translate a redirect like the one you shared into the Netlify equivalent, correctly deciding if the rewrite should be at the very top or the very bottom of the list.

Netlify only redirects/rewrites once per request

I need to double check this, but my understanding is that Netlify only redirects or rewrites once per request. All server-side rendered pages and all pages with dynamic paths already require one redirect. For example, we may have a SSRed page called /users. That page gets turned into a serverless function and next-on-netlify creates a redirect for /users to /.netlify/functions/next_users . Now, if we try to add a redirect for /allusers to /users, Netlify will perform only that redirect, but not the /users -> /.netlify/functions/next_users one. It will try to serve users.html from the publish directory (out_public), not find it, and return 404.

One way to correct for that would be to evaluate the redirects at build-time. For example: next-on-netlify sees a redirect for /allusers to /users and next-on-netlify knows that we need to redirect /users to /.netlify/functions/next_users, so next-on-netlify writes the redirect as /allusers > /.netlify/functions/next_users. But that only works if the redirects are static. As soon as they are dyamic, using /:params or *-splat, this kind of evaluation becomes very difficult/impossible.

Next Steps

What do you think? Do you have ideas for how this could work? In your specific case, you could just work around it for now: The first two redirects are not needed for Netlify (because they normalize the URLs and trailing slashes anyway) and the last two rewrites you could manually add as one single rewrite in your netlify.toml:

[[redirects]]
  from = "/*"
  to = "/.netlify/functions/next_api_autoRedirectToLocalisedPage"
  status = 200

It would catch any unmatched path and call your API endpoint. Not what you were hoping for, I'm sure, but that's my best idea at the moment.

Let me know what you think!
- Finn

from next-runtime.

TimNZ avatar TimNZ commented on May 18, 2024 1

To be clear: custom header in next.config.js are not supported?

headers: async () => {
	return [
		{
		source: "/(.*\\.(?:jpg|jpeg|png|gif))",
		headers: [
		{
			key: "Cache-Control",
			value: "public, max-age=3600",
		},
	],
	},
	]
	},

from next-runtime.

tbgse avatar tbgse commented on May 18, 2024 1

Hi all,

jumping on this issue because we're facing a possibly related problem: We want to create a rewrite to our api routes using the netlify.toml file, looking like this:

[[redirects]]
  from = "/custom-api-route/*"
  to = ""/api/:splat"
  status = 200

However, it seems like all of the requests to /custom-api/* end up with a 404 status. Possibly because the nextjs api routes itself are already configured as rewrites in the generated toml file by the nextjs plugin for netlify?

Is this a known issue or am I doing something wrong? When I configure non-api route redirects in the netlify.toml file, everything works as expected 🤔

from next-runtime.

ascorbic avatar ascorbic commented on May 18, 2024 1

Fixed in version 4

from next-runtime.

odenirdev avatar odenirdev commented on May 18, 2024

i have the same problem of @scopsy

from next-runtime.

odenirdev avatar odenirdev commented on May 18, 2024

i need the rewrites next.config.js

from next-runtime.

wildabeast avatar wildabeast commented on May 18, 2024

I cannot for the life of me get a CSP header set on the root page of my nextjs/netlify app.

next.config.js:

    async headers() {
      return [
        {
          source: '/',
          headers: [
            {
              key: 'Content-Security-Policy',
              value: "default-src 'self';",
            },
          ]
        }
      ];
    },

netlify.toml

[[headers]]
  for = "/*"
  [headers.values]
    Content-Security-Policy = "default-src 'self';"

_headers

/*
  Content-Security-Policy: default-src 'self';

Any guidance? thx

from next-runtime.

adshodgson avatar adshodgson commented on May 18, 2024

I cannot for the life of me get a CSP header set on the root page of my nextjs/netlify app.

next.config.js:

    async headers() {
      return [
        {
          source: '/',
          headers: [
            {
              key: 'Content-Security-Policy',
              value: "default-src 'self';",
            },
          ]
        }
      ];
    },

netlify.toml

[[headers]]
  for = "/*"
  [headers.values]
    Content-Security-Policy = "default-src 'self';"

_headers

/*
  Content-Security-Policy: default-src 'self';

Any guidance? thx

Having similar issue, any resolution?

from next-runtime.

Hexi1997 avatar Hexi1997 commented on May 18, 2024

I cannot for the life of me get a CSP header set on the root page of my nextjs/netlify app.
next.config.js:

    async headers() {
      return [
        {
          source: '/',
          headers: [
            {
              key: 'Content-Security-Policy',
              value: "default-src 'self';",
            },
          ]
        }
      ];
    },

netlify.toml

[[headers]]
  for = "/*"
  [headers.values]
    Content-Security-Policy = "default-src 'self';"

_headers

/*
  Content-Security-Policy: default-src 'self';

Any guidance? thx

Having similar issue, any resolution?

I cannot for the life of me get a CSP header set on the root page of my nextjs/netlify app.
next.config.js:

    async headers() {
      return [
        {
          source: '/',
          headers: [
            {
              key: 'Content-Security-Policy',
              value: "default-src 'self';",
            },
          ]
        }
      ];
    },

netlify.toml

[[headers]]
  for = "/*"
  [headers.values]
    Content-Security-Policy = "default-src 'self';"

_headers

/*
  Content-Security-Policy: default-src 'self';

Any guidance? thx

Having similar issue, any resolution?

Hey, Have you solved this problem? I encountered it too

from next-runtime.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.