Skip to content

feat: added better custom header support #1358

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

Merged
merged 24 commits into from
Jun 6, 2022

Conversation

nickytonline
Copy link

@nickytonline nickytonline commented May 24, 2022

Summary

Improves support for Next.js custom headers. Currently the headers() function in next.config.js only works for SSR/ISR. Headers in netlify.toml or a _headers file are completely ignored for SSR/ISR. For SSG pages, only the headers defined in netlify.toml or a _headers file are recognized and headers defined in headers() in next.config.js are ignored.

Previously, you had to manually copy from the headers() function to netlify.toml or vice-versa so all headers were applied. Now all that is required is defining custom headers in the headers() function of next.config.js. The build plugin will update the Netlify configuration at build time with all the custom headers defined in the headers() function of next.config.js.

You can still add headers in netlify.toml or _headers, but those additional headers will only apply to SSG pages.

@KyleBlankRollins, we'll need a documentation update for this as well. Happy to write this up, just not sure how we usually work with the documentation team.

Note that ISR and SSR are not affected by this plugin fix.

Test plan

The source code for test site can be found at https://github.com/nickytonline/next-plugin-issue-1346

Note that unless you load a page explicitly from the browser address bar, Next.js client-side routing will kick in so as you navigate to a new page, from the first-page load, you will not see the custom headers in subsequent pages that load from the client-side as the browser is not refreshing. You will need to refresh the browser to see the headers.

If you want to test the fix locally, you'll need to build the fix locally and run npm link, then in the test site repository, you'll need to run npm link @netlify/plugin-nextjs. Just a reminder that the fix can only be tested in a site that was deployed to Netlify using the plugin fix. There is no way to test this locally.

This is the configuration being used in the sites deployed in the test scenarios, https://github.com/nickytonline/next-plugin-issue-1346/blob/96471d10bb7be58287859e1e62af227eacf9e127/next.config.js

  1. First check with the existing plugin that headers aren't being served. Navigate to https://62991c0c8fa1390082a46ace--the-awesome-nick-taylor-site-69bab.netlify.app/some-base-path/.
  2. If you inspect the network tab for the request of the page rendered, you'll notice none of the custom headers I created are there, e.g. the x-all-pages-header header
  3. Navigate to the deployed site that used the plugin fix, https://629984ea303dfb0ed76ba332--the-awesome-nick-taylor-site-69bab.netlify.app/some-base-path/.
  4. Open the Network tab and you should see the following headers:
x-all-pages-header: all pages header value
x-all-pages-header2: all pages header value2
x-header-for-root-of-site: this is the header for the root of the site
x-header-for-root-of-site2: this is another the header for the root of the site

Network tab showing custom headers being served from the Netlify configuration

  1. Navigate to https://629984ea303dfb0ed76ba332--the-awesome-nick-taylor-site-69bab.netlify.app/some-base-path/en/some-path/yolo
  2. Open the Network tab and you should see the following headers:
x-all-locales-header-some-path: all locales header some path value
x-all-pages-header: all pages header value
x-all-pages-header2: all pages header value2
x-en-locale-specific-yolo-header: en locale specific yolo header value

Network tab showing custom headers being served from the Netlify configuration6

  1. Navigate to https://629984ea303dfb0ed76ba332--the-awesome-nick-taylor-site-69bab.netlify.app/some-base-path/en/some-path and https://629984ea303dfb0ed76ba332--the-awesome-nick-taylor-site-69bab.netlify.app/some-base-path/es/some-path
  2. Open the Network tab and you should see the following headers for each of those pages:
x-all-locales-header-some-path: all locales header some path value
x-all-pages-header: all pages header value
x-all-pages-header2: all pages header value2

Network tab showing custom headers being served from the Netlify configuration

Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal

Closes #1346

Standard checks:

  • Check the Deploy Preview's Demo site for your PR's functionality
  • Add docs when necessary

🧪 Once merged, make sure to update the version if needed and that it was published correctly.

A player heading a goal in a soccer match

@netlify
Copy link

netlify bot commented May 24, 2022

Deploy Preview for netlify-plugin-nextjs-demo ready!

Name Link
🔨 Latest commit d175a92
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/629e40d41bddbc00094bca2f
😎 Deploy Preview https://deploy-preview-1358--netlify-plugin-nextjs-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions github-actions bot added the type: feature code contributing to the implementation of a feature and/or user facing functionality label May 24, 2022
@netlify
Copy link

netlify bot commented May 24, 2022

Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!

Name Link
🔨 Latest commit d175a92
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/629e40d41ad07d00083cba0c
😎 Deploy Preview https://deploy-preview-1358--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 24, 2022

Deploy Preview for netlify-plugin-nextjs-export-demo ready!

Name Link
🔨 Latest commit d175a92
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/629e40d5cbc655000847589d
😎 Deploy Preview https://deploy-preview-1358--netlify-plugin-nextjs-export-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@nickytonline nickytonline requested a review from a team May 24, 2022 19:29
@netlify
Copy link

netlify bot commented May 24, 2022

Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!

Name Link
🔨 Latest commit d175a92
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/629e40d5859fab000857ea24
😎 Deploy Preview https://deploy-preview-1358--netlify-plugin-nextjs-static-root-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 24, 2022

Deploy Preview for next-plugin-rsc-demo ready!

Name Link
🔨 Latest commit d175a92
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-rsc-demo/deploys/629e40d57ab8150008e7cec6
😎 Deploy Preview https://deploy-preview-1358--next-plugin-rsc-demo.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 24, 2022

Deploy Preview for next-plugin-edge-middleware canceled.

Name Link
🔨 Latest commit d175a92
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/629e40d5e474790008a464fb

@netlify
Copy link

netlify bot commented May 24, 2022

Deploy Preview for next-i18next-demo canceled.

Name Link
🔨 Latest commit d175a92
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/629e40d5f3eebf0009b9ee1c

@netlify
Copy link

netlify bot commented May 24, 2022

Deploy Preview for netlify-plugin-nextjs-next-auth-demo canceled.

Name Link
🔨 Latest commit d175a92
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/629e40d51ad07d00083cba11

@cypress
Copy link

cypress bot commented May 24, 2022



Test summary

7 0 0 0


Run details

Project netlify-plugin-nextjs-static-demo
Status Passed
Commit 7092a6f ℹ️
Started Jun 6, 2022 6:04 PM
Ended Jun 6, 2022 6:05 PM
Duration 01:17 💡
OS Linux Ubuntu - 20.04
Browser Chrome 102

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@cypress
Copy link

cypress bot commented May 24, 2022



Test summary

21 0 0 0


Run details

Project netlify-plugin-nextjs-default-demo
Status Passed
Commit 7092a6f ℹ️
Started Jun 6, 2022 6:04 PM
Ended Jun 6, 2022 6:05 PM
Duration 01:28 💡
OS Linux Ubuntu - 20.04
Browser Chrome 102

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@nickytonline nickytonline force-pushed the nickytonline/feat-better-custom-header-support branch 2 times, most recently from cc82ac5 to af4e234 Compare May 24, 2022 21:04
@nickytonline nickytonline marked this pull request as ready for review May 24, 2022 22:03
@nickytonline
Copy link
Author

The failing tests are complaining about not finding a routes-manifest.json file, but I check if it exists before loading it. Will take a peek at this in the AM.

@nickytonline nickytonline marked this pull request as draft May 25, 2022 00:22
Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great. I think this approach is exactly the right one. Just a couple of comments and questions

@nickytonline nickytonline marked this pull request as ready for review May 25, 2022 12:00
@cypress
Copy link

cypress bot commented May 25, 2022



Test summary

2 0 0 0


Run details

Project netlify-plugin-nextjs-nx-monorepo-demo
Status Passed
Commit 7092a6f ℹ️
Started Jun 6, 2022 6:04 PM
Ended Jun 6, 2022 6:05 PM
Duration 01:13 💡
OS Linux Ubuntu - 20.04
Browser Chrome 102

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@nickytonline nickytonline marked this pull request as draft May 25, 2022 12:10
@KyleBlankRollins
Copy link

Thanks for tagging me, @nickytonline! You have several options for a docs update. I'll list them in order of how long it'll likely take to complete (shortest time to longest time).

  • Create a PR in the docs repo and update/add content as necessary to the Next.js page. I'll review it and suggest changes/edits.
  • Create an issue in the docs repo. I'll pick up the issue when I can and create my own PR, then tag you for review.
  • Wait for me to create an issue, then pick it up and create the PR and tag you for review.
    I mention this because I'll be slower than usual this week.

@nickytonline
Copy link
Author

nickytonline commented May 25, 2022

Thanks for tagging me, @nickytonline! You have several options for a docs update. I'll list them in order of how long it'll likely take to complete (shortest time to longest time).

  • Create a PR in the docs repo and update/add content as necessary to the Next.js page. I'll review it and suggest changes/edits.
  • Create an issue in the docs repo. I'll pick up the issue when I can and create my own PR, then tag you for review.
  • Wait for me to create an issue, then pick it up and create the PR and tag you for review.
    I mention this because I'll be slower than usual this week.

Thanks, @KyleBlankRollins! I'll create a PR to the docs repo for this.

@nickytonline nickytonline force-pushed the nickytonline/feat-better-custom-header-support branch from db53817 to 01df656 Compare May 25, 2022 14:25
@nickytonline
Copy link
Author

nickytonline commented May 25, 2022

TODO Add docs related to the PR https://github.com/netlify/docs/pull/2067

@nickytonline nickytonline force-pushed the nickytonline/feat-better-custom-header-support branch 2 times, most recently from 89f5b62 to 3acb29f Compare May 25, 2022 19:00
@nickytonline nickytonline marked this pull request as ready for review May 25, 2022 20:36
@nickytonline nickytonline force-pushed the nickytonline/feat-better-custom-header-support branch from 8de955e to e3c8ed1 Compare June 6, 2022 17:55
@nickytonline nickytonline requested a review from ericapisani June 6, 2022 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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