Skip to content

[Bug]: Homepage ignores the headers set in next.config.js when deployed to Netlify #1346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 2 tasks
meness opened this issue May 8, 2022 · 7 comments · Fixed by #1358
Closed
1 of 2 tasks

[Bug]: Homepage ignores the headers set in next.config.js when deployed to Netlify #1346

meness opened this issue May 8, 2022 · 7 comments · Fixed by #1358
Assignees
Labels
type: bug code to address defects in shipped code

Comments

@meness
Copy link

meness commented May 8, 2022

Summary

I set some headers in next.config.js as it's recommended on Next.js docs. You also mentioned the version 4 of this cool plugin has full support of Next.js headers, rewrites, and redirects on the release page, so I expect all the headers to be set, but It didn't happen for me. Netlify says this plugin is used and everything else works fine. Also, I tried setting NODE_ENV=production environment and redeploying, but got no luck.

Steps to reproduce

  1. Set headers in next.config.js
  2. Push changes to GitHub
  3. Netlify builds and deploys the project automatically
  4. Check the headers of the website using an online tool

A link to a reproduction repository

No response

Plugin version

4.7.0

More information about your build

  • I am building using the CLI
  • I am building using file-based configuration (netlify.toml)

What OS are you using?

Windows

Your netlify.toml file

`netlify.toml`
[context.production.environment]
NEXT_PUBLIC_X="X"

[context.staging.environment]
NEXT_PUBLIC_X="X"

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
const { withSentryConfig } = require('@sentry/nextjs');
const nextBundleAnalyzer = require('@next/bundle-analyzer');
const { i18n } = require('./next-i18next.config');

const securityHeaders = [
  {
    key: 'X-Content-Type-Options',
    value: 'nosniff',
  },
  {
    key: 'Content-Security-Policy',
    value: "default-src 'self';",
  },
  {
    key: 'Strict-Transport-Security',
    value: 'max-age=63072000; includeSubDomains; preload',
  },
  {
    key: 'Strict-Transport-Security',
    value: 'max-age=63072000; includeSubDomains; preload',
  },
  {
    key: 'Cross-Origin-Opener-Policy',
    value: 'same-origin',
  },
  {
    key: 'Cross-Origin-Resource-Policy',
    value: 'same-origin',
  },
];

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack(config, { webpack }) {
    config.module.rules.push({
      test: /\.svg$/,
      issuer: /\.tsx?$/,
      use: [
        {
          loader: '@svgr/webpack',
          options: {
            typescript: true,
            filenameCase: 'kebab',
            memo: true,
          },
        },
      ],
    });

    if (process.env.NODE_ENV === 'production') {
      config.plugins.push(
        new webpack.DefinePlugin({
          __SENTRY_DEBUG__: false,
        }),
      );
    }

    return config;
  },
  reactStrictMode: true,
  swcMinify: true,
  compiler: {
    removeConsole: process.env.NODE_ENV === 'production',
  },
  pageExtensions: [
    'page.tsx',
  ],
  async rewrites() {
    const rewrites = [
      { source: '/s/:slug*', destination: '/p/:slug*' },
      {
        source: '/api/:slug*',
        destination: 'backend_url:slug*',
      },
    ];

    return rewrites;
  },
  async redirects() {
    return [
      {
        source: '/p/:slug*',
        destination: '/s/:slug*',
        permanent: true,
      },
    ];
  },
  async headers() {
    if (process.env.NODE_ENV === 'production') {
      return [
        {
          source: '/:path*',
          headers: securityHeaders,
        },
      ];
    }

    return [];
  },
  i18n,
};

const withBundleAnalyzer = nextBundleAnalyzer({
  enabled: process.env.ANALYZE === 'true',
});

module.exports = withBundleAnalyzer(withSentryConfig(nextConfig));

Builds logs (or link to your logs)

Build logs
# Paste logs here

Function logs

Function logs
# Paste logs here

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.
@meness meness added the type: bug code to address defects in shipped code label May 8, 2022
@meness
Copy link
Author

meness commented May 10, 2022

I tried to remove the process.env.NODE_ENV check, but still the same

@meness meness changed the title [Bug]: Headers set in next.config.js won't be set when deployed to Netlify [Bug]: Headers set in next.config.js ignored when deployed to Netlify May 10, 2022
@meness
Copy link
Author

meness commented May 11, 2022

The issue here is source: '/:path*' doesn't match the homepage (/) when deployed while it works on localhost. I let this issue be open to make sure the issue is not related to the plugin.

@meness
Copy link
Author

meness commented May 11, 2022

I tried source: '/(.*?)' but still the homepage ignores the headers when deployed.

@meness meness changed the title [Bug]: Headers set in next.config.js ignored when deployed to Netlify [Bug]: Homepage ignores the headers set in next.config.js when deployed to Netlify May 12, 2022
@nickytonline nickytonline self-assigned this May 20, 2022
@bhavana-netlify
Copy link

Hi @nickytonline looks like the work to fix the bug is done, but we are waiting on docs and some testing to complete before we can close this? is that accurate?
I;d love an update from you re: current state and what is remaining + timeline so I can let the team at Estee know. thank you.

@nickytonline
Copy link

Hi @bhavana-netlify, messaged you on Slack. This should be getting merged today.

@kodiakhq kodiakhq bot closed this as completed in #1358 Jun 6, 2022
@meness
Copy link
Author

meness commented Jun 19, 2022

@nickytonline The problem still exists on the latest version

@nickytonline
Copy link

@meness, do you have a repository I can pull down to see why you may be having the issue still? We have tests that check that it's working, so maybe there is an edge case we missed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants