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

Update to skip middleware for unstable_revalidate #37734

Merged
merged 2 commits into from
Jun 16, 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
9 changes: 9 additions & 0 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import ResponseCache from '../server/response-cache'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { getNextPathnameInfo } from '../shared/lib/router/utils/get-next-pathname-info'
import { bodyStreamToNodeStream, clonableBodyForRequest } from './body-streams'
import { checkIsManualRevalidate } from './api-utils'

const shouldUseReactRoot = parseInt(React.version) >= 18
if (shouldUseReactRoot) {
Expand Down Expand Up @@ -1137,6 +1138,14 @@ export default class NextNodeServer extends BaseServer {
onWarning?: (warning: Error) => void
}) {
middlewareBetaWarning()

// middleware is skipped for on-demand revalidate requests
if (
checkIsManualRevalidate(params.request, this.renderOpts.previewProps)
.isManualRevalidate
) {
return { finished: false }
}
const normalizedPathname = removeTrailingSlash(params.parsed.pathname || '')

// For middleware to "fetch" we must always provide an absolute URL
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/middleware-general/app/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const params = (url) => {
export async function middleware(request) {
const url = request.nextUrl

if (request.headers.get('x-prerender-revalidate')) {
const res = NextResponse.next()
res.headers.set('x-middleware', 'hi')
return res
}

// this is needed for tests to get the BUILD_ID
if (url.pathname.startsWith('/_next/static/__BUILD_ID')) {
return NextResponse.next()
Expand Down
30 changes: 30 additions & 0 deletions test/e2e/middleware-general/app/pages/ssg/[slug].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useRouter } from 'next/router'

export default function Page(props) {
const router = useRouter()
return (
<>
<p id="blog">/blog/[slug]</p>
<p id="query">{JSON.stringify(router.query)}</p>
<p id="pathname">{router.pathname}</p>
<p id="as-path">{router.asPath}</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}

export function getStaticProps({ params }) {
return {
props: {
now: Date.now(),
params,
},
}
}

export function getStaticPaths() {
return {
paths: ['/ssg/first'],
fallback: 'blocking',
}
}
15 changes: 15 additions & 0 deletions test/e2e/middleware-general/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ describe('Middleware Runtime', () => {
)
}
})

it('should not run middleware for on-demand revalidate', async () => {
const bypassToken = (
await fs.readJSON(join(next.testDir, '.next/prerender-manifest.json'))
).preview.previewModeId

const res = await fetchViaHTTP(next.url, '/ssg/first', undefined, {
headers: {
'x-prerender-revalidate': bypassToken,
},
})
expect(res.status).toBe(200)
expect(res.headers.get('x-middleware')).toBeFalsy()
expect(res.headers.get('x-nextjs-cache')).toBe('REVALIDATED')
})
}

it('should have correct dynamic route params on client-transition to dynamic route', async () => {
Expand Down