-
Notifications
You must be signed in to change notification settings - Fork 86
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
feat: support after() #2717
Merged
Merged
feat: support after() #2717
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d5aca78
test: upgrade deno used in tests
pieh beec061
test: e2e simple next/after test
pieh e13fa25
feat: support waitUntil
ascorbic 8352645
use already existing trackBackgroundWork helper from request context …
pieh 81583b7
test: increase timeout for one of smoke tests due to team-wide extens…
pieh 819be98
test: move test to dedicated fixture
pieh 80de44f
chore: update outdated comment
pieh c938bb6
chore: clarify awaiting backgroundWorkPromise
pieh 1a8a135
test: update assertion for next >=15.0.4-canary.18
pieh 7217c6b
fix: support changed shape of getRequestHandlers return
pieh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getRequestContext } from './request-context.cjs' | ||
|
||
/** | ||
* @see https://github.com/vercel/next.js/blob/canary/packages/next/src/server/after/builtin-request-context.ts | ||
*/ | ||
const NEXT_REQUEST_CONTEXT_SYMBOL = Symbol.for('@next/request-context') | ||
|
||
export type NextJsRequestContext = { | ||
get(): { waitUntil?: (promise: Promise<unknown>) => void } | undefined | ||
} | ||
|
||
type GlobalThisWithRequestContext = typeof globalThis & { | ||
[NEXT_REQUEST_CONTEXT_SYMBOL]?: NextJsRequestContext | ||
} | ||
|
||
/** | ||
* Registers a `waitUntil` to be used by Next.js for next/after | ||
*/ | ||
export function setupWaitUntil() { | ||
// eslint-disable-next-line @typescript-eslint/no-extra-semi | ||
;(globalThis as GlobalThisWithRequestContext)[NEXT_REQUEST_CONTEXT_SYMBOL] = { | ||
get() { | ||
return { waitUntil: getRequestContext()?.trackBackgroundWork } | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { expect } from '@playwright/test' | ||
import { nextVersionSatisfies } from '../utils/next-version-helpers.mjs' | ||
import { test } from '../utils/playwright-helpers.js' | ||
|
||
test('next/after callback is executed and finishes', async ({ page, after }) => { | ||
test.skip(!nextVersionSatisfies('>=15.0.0'), 'This test is only for Next.js 15+') | ||
|
||
// trigger initial request to check page which might be stale and allow regenerating in background | ||
await page.goto(`${after.url}/after/check`) | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 5000)) | ||
|
||
// after it was possibly regenerated we can start checking actual content of the page | ||
await page.goto(`${after.url}/after/check`) | ||
const pageInfoLocator1 = await page.locator('#page-info') | ||
const pageInfo1 = JSON.parse((await pageInfoLocator1.textContent()) ?? '{}') | ||
|
||
expect(typeof pageInfo1?.timestamp, 'Check page should have timestamp').toBe('number') | ||
|
||
await page.goto(`${after.url}/after/check`) | ||
const pageInfoLocator2 = await page.locator('#page-info') | ||
const pageInfo2 = JSON.parse((await pageInfoLocator2.textContent()) ?? '{}') | ||
|
||
expect(typeof pageInfo2?.timestamp, 'Check page should have timestamp').toBe('number') | ||
|
||
expect(pageInfo2.timestamp, 'Check page should be cached').toBe(pageInfo1.timestamp) | ||
|
||
await page.goto(`${after.url}/after/trigger`) | ||
|
||
// wait for next/after to trigger revalidation of check page | ||
await new Promise((resolve) => setTimeout(resolve, 5000)) | ||
|
||
await page.goto(`${after.url}/after/check`) | ||
const pageInfoLocator3 = await page.locator('#page-info') | ||
const pageInfo3 = JSON.parse((await pageInfoLocator3.textContent()) ?? '{}') | ||
|
||
expect(typeof pageInfo3?.timestamp, 'Check page should have timestamp').toBe('number') | ||
expect( | ||
pageInfo3.timestamp, | ||
'Check page should be invalidated with newer timestamp', | ||
).toBeGreaterThan(pageInfo1.timestamp) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const revalidate = 3600 // arbitrarily long, just so that it doesn't happen during a test run | ||
|
||
export default async function Page() { | ||
const data = { | ||
timestamp: Date.now(), | ||
} | ||
console.log('/timestamp/key/[key] rendered', data) | ||
|
||
return <div id="page-info">{JSON.stringify(data)}</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { revalidatePath } from 'next/cache' | ||
import { unstable_after as after, connection } from 'next/server' | ||
|
||
export default async function Page() { | ||
await connection() | ||
after(async () => { | ||
// this will run after response was sent | ||
console.log('after() triggered') | ||
console.log('after() sleep 1s') | ||
await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
|
||
console.log('after() revalidatePath /after/check') | ||
revalidatePath('/after/check') | ||
}) | ||
|
||
return <div>Page with after()</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const metadata = { | ||
title: 'Simple Next App', | ||
description: 'Description for Simple Next App', | ||
} | ||
|
||
export default function RootLayout({ children }) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
output: 'standalone', | ||
eslint: { | ||
ignoreDuringBuilds: true, | ||
}, | ||
experimental: { | ||
after: true, | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "after", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"postinstall": "next build", | ||
"dev": "next dev", | ||
"build": "next build" | ||
}, | ||
"dependencies": { | ||
"next": "latest", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0" | ||
}, | ||
"test": { | ||
"dependencies": { | ||
"next": ">=15.0.0" | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to support callback variant of
after()
:This is not needed to support promise variant like so: