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

docs: documents middleware matcher #40180

Merged
merged 3 commits into from
Sep 5, 2022

Conversation

feugy
Copy link
Member

@feugy feugy commented Sep 2, 2022

📖 What's in there?

Middleware matchers are powerful, but very few people realized it, because they are not really documented.
This PR tries to bring more clarity, and includes a more advanced example.

The example shows how to exclude several pages (no /static, no /public), but also allow specific page in excluded paths (/public/disclaimer)

🧪 How to test?

Run the example: pnpm next dev examples/middleware-matcher, then browse to http://localhost:3000
The first 3 links should not match, the last 3 ones should.

Don't forget to clear your localhost cookies if you change the middleware code.

🆙 Note to reviewers

Using session cookies to pass information from middleware to the rendered page is not great, because document.cookie is not available during SSR, and because cookies persist when refreshing the page (making it hard to try different matchers)

However, I couldn't find a simpler way to convey the information from the middleware to the page, and I meant to have something visual. The other option is to use response headers and curl commands, but...

@ijjk ijjk added area: documentation examples Issue was opened via the examples template. labels Sep 2, 2022
@feugy feugy force-pushed the docs/document-middleware-matcher branch from d4b3643 to 47673e5 Compare September 2, 2022 14:50
@feugy feugy force-pushed the docs/document-middleware-matcher branch from 47673e5 to ac301b8 Compare September 2, 2022 14:52
@kodiakhq kodiakhq bot merged commit 866de41 into vercel:canary Sep 5, 2022
atilafassina pushed a commit to atilafassina/next.js that referenced this pull request Sep 5, 2022
### 📖 What's in there?

Middleware matchers are powerful, but very few people realized it, because they are not really documented.
This PR tries to bring more clarity, and includes a more advanced example.

The example shows how to exclude several pages (no `/static`, no `/public`), but also allow specific page in excluded paths (`/public/disclaimer`)

### 🧪 How to test?

Run the example: `pnpm next dev examples/middleware-matcher`, then browse to http://localhost:3000
The first 3 links should not match, the last 3 ones should.

Don't forget to clear your localhost cookies if you change the middleware code.

### 🆙 Note to reviewers

Using session cookies to pass information from middleware to the rendered page is not great, because `document.cookie` is not available during SSR, and because cookies persist when refreshing the page (making it hard to try different matchers)

However, I couldn't find a simpler way to convey the information from the middleware to the page, and I meant to have something visual. The other option is to use response headers and curl commands, but...
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue was opened via the examples template.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants