Skip to content

feat: don't move files to CDN if they match middleware #812

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 3 commits into from
Nov 19, 2021

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Nov 18, 2021

Summary

When static file moving is enabled, pre-rendered pages are moved to the CDN rather than being served from the origin with next/server. This causes a problem if the site uses middleware, as it does not run on pages served from the CDN. This PR checks any moved files to see if they match any middleware. If they do, it does not move them to the CDN and instead serves them from the origin. It also logs a warning about this.

Test plan

  • Deploy the site with EXPERIMENTAL_MOVE_STATIC_PAGES.
  • Load /middle
  • Confirm that the message says middleware is enabled

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

Fixes #802

Standard checks:

  • Check the Deploy Preview's Demo site for your PR's functionality

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

@github-actions github-actions bot added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Nov 18, 2021
}

const matchMiddleware = (middleware, filePath) =>
middleware.includes('') ||
Copy link
Member

Choose a reason for hiding this comment

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

I don't have enough context to understand this check. Can you share an example of the shape middleware can take?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, never mind. I think I figured it out by looking at the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's an array of paths, relative to the site root, that indicate directories which have middleware. Next.js applies middleware to any path below that. The leading slash has been stripped. There are some examples in the unit tests. An empty string indicates root-level middleware thata applies to everything

eduardoboucas
eduardoboucas previously approved these changes Nov 19, 2021
Copy link
Member

@eduardoboucas eduardoboucas left a comment

Choose a reason for hiding this comment

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

LGTM! Left minor comments, none blocking.

console.log(
yellowBright(outdent`Skipped moving ${matchedPages.size} ${
matchedPages.size === 1 ? 'file because it matches' : 'files because they match'
} middleware, so cannot be deployed to the CDN and will be served from the origin instead. This is fine, but we're letting you know because it may not be what you expect.
Copy link
Member

Choose a reason for hiding this comment

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

👌🏻


`,
)
// There could potentially be thousands of matching pages, so we don't want to spam the console with this
Copy link
Member

Choose a reason for hiding this comment

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

Is it fine not to show any pages at all when this happens? Is it worth showing something like "The following files and X others matched middlware (...)", or is that overkill?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, possibly, though I'm not sure if that's useful information.

}

const matchMiddleware = (middleware, filePath) =>
middleware.includes('') ||
Copy link
Member

Choose a reason for hiding this comment

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

Ah, never mind. I think I figured it out by looking at the tests.

@kodiakhq kodiakhq bot merged commit 615c97a into main Nov 19, 2021
@kodiakhq kodiakhq bot deleted the mk/static-middleware-handling branch November 19, 2021 12:04
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.

Disable static page serving for routes covered by middleware
2 participants