Skip to content
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

fix: don't use ODB for routes that match middleware #1171

Merged
merged 7 commits into from
Feb 2, 2022

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Jan 27, 2022

Summary

Routes that match middleware need to be served via SSR, because the middleware is run at the origin. This PR ensures that any routes that matches mdidleware is not served by an ODB, which was causing errors where results were persisted including the middleware.

Test plan

  1. Visit the Deploy Preview
  2. Check the network tab: there should be a header x-middleware-date inserted by middleware. Ensure that the date is up to date. Also ensure that x-nf-render-mode is ssr.

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

Fixes #1168, Fixes #1114

capybara

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.

@netlify
Copy link

netlify bot commented Jan 27, 2022

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

🔨 Explore the source changes: 2e9ddb4

🔍 Inspect the deploy log: https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/61fa7ff829ab9e0008e549b4

😎 Browse the preview: https://deploy-preview-1171--netlify-plugin-nextjs-demo.netlify.app

@netlify
Copy link

netlify bot commented Jan 27, 2022

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

🔨 Explore the source changes: 2e9ddb4

🔍 Inspect the deploy log: https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/61fa7ff859152d0007755cb4

😎 Browse the preview: https://deploy-preview-1171--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app

@netlify
Copy link

netlify bot commented Jan 27, 2022

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

🔨 Explore the source changes: 2e9ddb4

🔍 Inspect the deploy log: https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/61fa7ff81a639d000747d034

😎 Browse the preview: https://deploy-preview-1171--netlify-plugin-nextjs-export-demo.netlify.app

@netlify
Copy link

netlify bot commented Jan 27, 2022

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

🔨 Explore the source changes: 2e9ddb4

🔍 Inspect the deploy log: https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/61fa7ff831fed20008fe6468

😎 Browse the preview: https://deploy-preview-1171--netlify-plugin-nextjs-static-root-demo.netlify.app

@github-actions github-actions bot added the type: bug code to address defects in shipped code label Jan 27, 2022
@cypress
Copy link

cypress bot commented Jan 27, 2022



Test summary

2 0 0 0


Run details

Project netlify-plugin-nextjs-nx-monorepo-demo
Status Passed
Commit 402ca22 ℹ️
Started Feb 2, 2022 1:01 PM
Ended Feb 2, 2022 1:02 PM
Duration 01:09 💡
OS Linux Ubuntu - 20.04
Browser Chrome 97

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 Jan 27, 2022



Test summary

7 0 0 0


Run details

Project netlify-plugin-nextjs-static-demo
Status Passed
Commit 402ca22 ℹ️
Started Feb 2, 2022 1:01 PM
Ended Feb 2, 2022 1:02 PM
Duration 01:13 💡
OS Linux Ubuntu - 20.04
Browser Chrome 97

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 Jan 27, 2022



Test summary

17 0 0 0


Run details

Project netlify-plugin-nextjs-default-demo
Status Passed
Commit 402ca22 ℹ️
Started Feb 2, 2022 11:11 AM
Ended Feb 2, 2022 11:13 AM
Duration 01:27 💡
OS Linux Ubuntu - 20.04
Browser Chrome 97

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

@ascorbic ascorbic force-pushed the mk/middleware-rewrites branch 4 times, most recently from 0772ede to 685661c Compare January 27, 2022 13:51
@ascorbic ascorbic force-pushed the mk/middleware-rewrites branch from 685661c to c6ff51f Compare January 27, 2022 14:14
@ascorbic ascorbic changed the title fix: wip don't use ODB for routes that match middleware fix: don't use ODB for routes that match middleware Jan 28, 2022
@ascorbic ascorbic marked this pull request as ready for review January 28, 2022 14:57
@ascorbic ascorbic requested a review from tiffafoo January 28, 2022 14:57
? `is one statically-generated or ISR route`
: `are ${middlewareMatches} statically-generated or ISR routes`
} that match a middleware function, which means they will always be served from the SSR function and will not use ISR or be served from the CDN.
If this was not intended, ensure that your middleware only matches routes that you intend to use SSR.
Copy link

Choose a reason for hiding this comment

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

cc @KyleBlankRollins for this message 😊

...middleware
.map((route) => {
const unlocalized = [handlerRewrite(`${route}`), handlerRewrite(`${route}/*`)]
if (i18n?.locales?.length > 0) {
Copy link

Choose a reason for hiding this comment

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

Suggested change
if (i18n?.locales?.length > 0) {
if (i18n?.locales?.length) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Explicitly testing against zero is in the linting rules

.map((route) => {
const unlocalized = [handlerRewrite(`${route}`), handlerRewrite(`${route}/*`)]
if (i18n?.locales?.length > 0) {
const localized = i18n?.locales?.map((locale) => [
Copy link

Choose a reason for hiding this comment

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

Suggested change
const localized = i18n?.locales?.map((locale) => [
const localized = i18n.locales.map((locale) => [

Since the above check prevents it, you don't need to have the optional check here

@@ -65,6 +71,7 @@ export const generateStaticRedirects = ({
}
}

// eslint-disable-next-line max-lines-per-function
Copy link

Choose a reason for hiding this comment

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

[sand] This function is indeed starting to get pretty complex. Could you split it out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'\ve split out the various bits

} from './utils'

const matchesMiddleware = (middleware: Array<string>, route: string): boolean =>
Copy link

Choose a reason for hiding this comment

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

Suggested change
const matchesMiddleware = (middleware: Array<string>, route: string): boolean =>
const matchesMiddleware = (middleware: Array<string> | undefined, route: string): boolean =>

[sand] Or optional? I see you check the existence in the line below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like middleware is always defined, because getMiddleware defaults to [], so I'll just remove the optional chain check

export const getMiddleware = async (publish: string): Promise<Array<string>> => {
const manifestPath = join(publish, 'server', 'middleware-manifest.json')
if (existsSync(manifestPath)) {
const manifest = await readJson(manifestPath)
Copy link

Choose a reason for hiding this comment

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

No error handling? It looks like readJSON also allows you to pass it { throws: false } so it doesn't throw if it fails to read the JSON.

tiffafoo
tiffafoo previously approved these changes Feb 1, 2022
Copy link

@tiffafoo tiffafoo left a comment

Choose a reason for hiding this comment

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

Some lil comments!

Copy link

@KyleBlankRollins KyleBlankRollins left a comment

Choose a reason for hiding this comment

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

Thanks for tagging me, @tiffanosaurus! @ascorbic, I have a couple edits, but wouldn't consider them blocking.

Copy link

@tiffafoo tiffafoo left a comment

Choose a reason for hiding this comment

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

LGTM, I added some comments for return types

@ascorbic
Copy link
Contributor Author

ascorbic commented Feb 2, 2022

@tiffanosaurus Added types, thanks!

@ascorbic ascorbic requested a review from tiffafoo February 2, 2022 11:07
@kodiakhq kodiakhq bot merged commit bbcdfbd into main Feb 2, 2022
@kodiakhq kodiakhq bot deleted the mk/middleware-rewrites branch February 2, 2022 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: bug code to address defects in shipped code
Projects
None yet
3 participants