NextJS plugin adds hundreds of redirect rules to our site

We are using NextJS, and the build logs is showing that Netlify is adding hundreds of redirect rules through its NextJS plugin.

A few of the rules make clear sense - i.e. redirecting /api to the function handler, and redirecting some links to 404.
But most of the rules just look like this - one rule per file in /public:

 {
    from: '/path/in/public',
    to: '/path/in/public',
    conditions: { Cookie: [Array] },
    status: 200
 }

These rules seem redundant, and they definitely clutter the build log.

Can anybody explain what they are for, if they can be rid of, and if so - how?
Thanks

Hey there, @frogger77 :wave:

Could you share a full version of your build log? This will help us look into the matter further. Thanks so much!

Here is the file up to where the redirects are seen:

12:30:44 AM: Build ready to start
12:30:46 AM: build-image version: 122b31996ccaffd45d820a452d6227f8312110cc (focal)
12:30:46 AM: build-image tag: v4.5.3
12:30:46 AM: buildbot version: 45faaf324677495f3400b572da79977ab3212265
12:30:46 AM: Fetching cached dependencies
12:30:47 AM: Starting to download cache of 1.1GB
12:30:52 AM: Finished downloading cache in 5.478743012s
12:30:52 AM: Starting to extract cache
12:31:18 AM: Finished extracting cache in 25.947530829s
12:31:18 AM: Finished fetching cache in 31.595118805s
12:31:18 AM: Starting to prepare the repo for build
12:31:18 AM: Preparing Git Reference pull/43/head
12:31:20 AM: Parsing package.json dependencies
12:31:20 AM: Starting build script
12:31:20 AM: Installing dependencies
12:31:20 AM: Python version set to 2.7
12:31:21 AM: Started restoring cached node version
12:31:23 AM: Finished restoring cached node version
12:31:23 AM: v16.13.2 is already installed.
12:31:24 AM: Now using node v16.13.2 (npm v8.1.2)
12:31:24 AM: Started restoring cached build plugins
12:31:24 AM: Finished restoring cached build plugins
12:31:24 AM: Attempting ruby version 2.7.2, read from environment
12:31:25 AM: Using ruby version 2.7.2
12:31:25 AM: Using PHP version 8.0
12:31:25 AM: Started restoring cached yarn cache
12:31:33 AM: Finished restoring cached yarn cache
12:31:33 AM: No yarn workspaces detected
12:31:33 AM: Started restoring cached node modules
12:31:33 AM: Finished restoring cached node modules
12:31:33 AM: Installing NPM modules using Yarn version 1.22.10
12:31:34 AM: yarn install v1.22.10
12:31:34 AM: [1/4] Resolving packages...
12:31:35 AM: success Already up-to-date.
12:31:35 AM: Done in 0.55s.
12:31:35 AM: NPM modules installed using Yarn
12:31:35 AM: Started restoring cached go cache
12:31:35 AM: Finished restoring cached go cache
12:31:35 AM: go version go1.16.5 linux/amd64
12:31:35 AM: go version go1.16.5 linux/amd64
12:31:35 AM: Installing missing commands
12:31:35 AM: Verify run directory
12:31:36 AM: ​
12:31:36 AM: ────────────────────────────────────────────────────────────────
12:31:36 AM:   Netlify Build                                                 
12:31:36 AM: ────────────────────────────────────────────────────────────────
12:31:36 AM: ​
12:31:36 AM: ❯ Version
12:31:36 AM:   @netlify/build 26.2.2
12:31:36 AM: ​
12:31:36 AM: ❯ Flags
12:31:36 AM:   baseRelDir: true
12:31:36 AM:   buildId: 61f46e934605cf0008f2cce0
12:31:36 AM:   deployId: 61f46e934605cf0008f2cce2
12:31:36 AM: ​
12:31:36 AM: ❯ Current directory
12:31:36 AM:   /opt/build/repo
12:31:36 AM: ​
12:31:36 AM: ❯ Config file
12:31:36 AM:   /opt/build/repo/netlify.toml
12:31:36 AM: ​
12:31:36 AM: ❯ Context
12:31:36 AM:   deploy-preview
12:31:37 AM: ​
12:31:37 AM: ❯ Loading plugins
12:31:37 AM:    - @netlify/plugin-nextjs@4.2.1 from Netlify app
12:31:38 AM: ​
12:31:38 AM: ────────────────────────────────────────────────────────────────
12:31:38 AM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
12:31:38 AM: ────────────────────────────────────────────────────────────────
12:31:38 AM: ​
12:31:38 AM: Next.js cache restored.
12:31:38 AM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
12:31:38 AM: ​
12:31:38 AM: (@netlify/plugin-nextjs onPreBuild completed in 351ms)
12:31:38 AM: ​
12:31:38 AM: ────────────────────────────────────────────────────────────────
12:31:38 AM:   2. build.command from netlify.toml                            
12:31:38 AM: ────────────────────────────────────────────────────────────────
12:31:38 AM: ​
12:31:38 AM: $ yarn build
12:31:39 AM: yarn run v1.22.10
12:31:39 AM: $ next build

... build logs here...

12:31:43 AM: info  - Creating an optimized production build...
12:31:44 AM: info  - Disabled SWC as replacement for Babel because of custom Babel configuration ".babelrc" https://nextjs.org/docs/messages/swc-disabled
12:31:45 AM: info  - Using external babel configuration from /opt/build/repo/.babelrc
12:32:00 AM: info  - Compiled successfully
12:32:00 AM: info  - Collecting page data...
12:32:03 AM: info  - Generating static pages (0/3)
12:32:03 AM: info  - Generating static pages (3/3)
12:32:03 AM: info  - Finalizing page optimization...
12:32:03 AM: Page                                       Size     First Load JS

... list of pages here....

12:32:03 AM: λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
12:32:03 AM: ○  (Static)  automatically rendered as static HTML (uses no initial props)
12:32:03 AM: $ next-sitemap
12:32:05 AM: Done in 26.25s.
12:32:05 AM: ​
12:32:05 AM: (build.command completed in 26.4s)
12:32:05 AM: ────────────────────────────────────────────────────────────────
12:32:05 AM:   3. @netlify/plugin-nextjs (onBuild event)                     
12:32:05 AM: ────────────────────────────────────────────────────────────────
12:32:05 AM: ​
12:32:05 AM: Patching /opt/build/repo/node_modules/next/dist/server/next-server.js
12:32:05 AM: Moving static page files to serve from CDN...
12:32:05 AM: Moved 1 files
12:32:05 AM: Netlify configuration property "redirects" value changed to [
12:32:05 AM:   { from: '/_next/static/*', to: '/static/:splat', status: 200 },
12:32:05 AM:   {
12:32:05 AM:     from: '/_next/image*',
12:32:05 AM:     query: { url: ':url', w: ':width', q: ':quality' },
12:32:05 AM:     to: '/_ipx/w_:width,q_:quality/:url',
12:32:05 AM:     status: 301
12:32:05 AM:   },
12:32:05 AM:   { from: '/_ipx/*', to: '/.netlify/builders/_ipx', status: 200 },
12:32:05 AM:   { from: '/cache/*', to: '/404.html', status: 404, force: true },
12:32:05 AM:   { from: '/server/*', to: '/404.html', status: 404, force: true },
12:32:05 AM:   { from: '/serverless/*', to: '/404.html', status: 404, force: true },
12:32:05 AM:   { from: '/traces', to: '/404.html', status: 404, force: true },
12:32:05 AM:   {
12:32:05 AM:     from: '/routes-manifest.json',
12:32:05 AM:     to: '/404.html',
12:32:05 AM:     status: 404,
12:32:05 AM:     force: true
12:32:05 AM:   },
12:32:05 AM:   {
12:32:05 AM:     from: '/build-manifest.json',
12:32:05 AM:     to: '/404.html',
12:32:05 AM:     status: 404,
12:32:05 AM:     force: true
12:32:05 AM:   },
12:32:05 AM:   {
12:32:05 AM:     from: '/prerender-manifest.json',
12:32:05 AM:     to: '/404.html',
12:32:05 AM:     status: 404,
12:32:05 AM:     force: true
12:32:05 AM:   },
12:32:05 AM:   {
12:32:05 AM:     from: '/react-loadable-manifest.json',
12:32:05 AM:     to: '/404.html',
12:32:05 AM:     status: 404,
12:32:05 AM:     force: true
12:32:05 AM:   },
12:32:05 AM:   { from: '/BUILD_ID', to: '/404.html', status: 404, force: true },
12:32:05 AM:   {
12:32:05 AM:     from: '/api',
12:32:05 AM:     to: '/.netlify/functions/___netlify-handler',
12:32:05 AM:     status: 200
12:32:05 AM:   },
12:32:05 AM:   {
12:32:05 AM:     from: '/api/*',
12:32:05 AM:     to: '/.netlify/functions/___netlify-handler',
12:32:05 AM:     status: 200
12:32:05 AM:   },
12:32:05 AM:   {
12:32:05 AM:     from: '/apple-touch-icon.png',
12:32:05 AM:     to: '/apple-touch-icon.png',
12:32:05 AM:     conditions: { Cookie: [Array] },
12:32:05 AM:     status: 200
12:32:05 AM:   },
12:32:05 AM:   {
12:32:05 AM:     from: '/browserconfig.xml',
12:32:05 AM:     to: '/browserconfig.xml',
12:32:05 AM:     conditions: { Cookie: [Array] },
12:32:05 AM:     status: 200
12:32:05 AM:   },
12:32:05 AM:   {
12:32:05 AM:     from: '/favicon-16x16.png',
12:32:05 AM:     to: '/favicon-16x16.png',
12:32:05 AM:     conditions: { Cookie: [Array] },
12:32:05 AM:     status: 200
12:32:05 AM:   },


... followed by many more like the last 3 items, apparently every file in public/ gets a redirect.

12:32:05 AM:   ... 311 more items
12:32:05 AM: ].
12:32:05 AM: ​
12:32:05 AM: (@netlify/plugin-nextjs onBuild completed in 420ms)
12:32:05 AM: ​
12:32:05 AM: ────────────────────────────────────────────────────────────────
12:32:05 AM:   4. Functions bundling                                         
12:32:05 AM: ────────────────────────────────────────────────────────────────
12:32:05 AM: ​
12:32:05 AM: Packaging Functions from .netlify/functions-internal directory:
12:32:05 AM:  - ___netlify-handler/___netlify-handler.js
12:32:05 AM:  - ___netlify-odb-handler/___netlify-odb-handler.js
12:32:05 AM:  - _ipx/_ipx.js

None of the rule is redundant and more or less all of them are required for the correct functioning of the site. Nextjs bypasses pre-rendering based on the presence of a cookie. That’s why there are 2 rules for each. Bypass a rule if that cookie is present or follow it.

Thanks, but not sure I understand.
Pre-rendering is not relevant for resources in /public, and certainly for non-HTML resources such as images.
What am I missing?

Hey @frogger77,

We just confirmed with the developers, and they are certain that all of those redirects are absolutely required. The developers have already lodged a concern about this polluting the build logs, so you’re not the only one who doesn’t like to see this, but this can change only if/when we’re able to make more structural changes to support Next.js.