Skip to content

fix: pass x-middleware-rewrite header to client #1322

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 2 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 2 additions & 48 deletions demos/middleware/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Head from 'next/head'
import Image from 'next/image'
import Link from 'next/link'
import styles from '../styles/Home.module.css'

export default function Home() {
Expand All @@ -16,54 +16,8 @@ export default function Home() {
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>

<p className={styles.description}>
Get started by editing{' '}
<code className={styles.code}>pages/index.js</code>
</p>

<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h2>Documentation &rarr;</h2>
<p>Find in-depth information about Next.js features and API.</p>
</a>

<a href="https://nextjs.org/learn" className={styles.card}>
<h2>Learn &rarr;</h2>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>

<a
href="https://github.com/vercel/next.js/tree/canary/examples"
className={styles.card}
>
<h2>Examples &rarr;</h2>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>

<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
>
<h2>Deploy &rarr;</h2>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
<p><Link href="/shows/rewriteme">Rewrite me</Link></p>
</main>

<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span className={styles.logo}>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
</footer>
</div>
)
}
9 changes: 9 additions & 0 deletions demos/middleware/pages/shows/rewriteme/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Show = () => {
return (
<div>
<p>This should have been rewritten</p>
</div>
)
}

export default Show
2 changes: 0 additions & 2 deletions plugin/src/templates/edge/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ export const buildResponse = async ({
request.headers.set('x-nf-next-middleware', 'skip')
const rewrite = res.headers.get('x-middleware-rewrite')
if (rewrite) {
res.headers.delete('x-middleware-rewrite')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the bit that matters

return addMiddlewareHeaders(context.rewrite(rewrite), res)
}
if (res.headers.get('x-middleware-next') === '1') {
res.headers.delete('x-middleware-next')
return addMiddlewareHeaders(context.next(), res)
}
return res
Expand Down