-
Notifications
You must be signed in to change notification settings - Fork 73
introduce new initOpenNextCloudflareForDev
utility and make getCloudflareContext
synchronous
#265
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
dario-piotrowicz
merged 20 commits into
main
from
dario/226/cloudflare-context-middleware
Jan 27, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
60a6296
introduce new `initOpenNextCloudflareForDev` utility and make `getClo…
dario-piotrowicz 30a9f89
Apply suggestions from code review
dario-piotrowicz a9b6b3a
remove `getInternalGlobalThis` function
dario-piotrowicz 26ae6de
mention that `initOpenNextCloudflareForDev` doesn't need to be `await`ed
dario-piotrowicz 6c29ca7
remove `init` awaiting in kvCache
dario-piotrowicz 6c54ae3
update test so that ordering of env keys does not matter
dario-piotrowicz c91b369
fixup! update kvCache
vicb 6e9e500
fixup! drop async
vicb a3b4551
remove `getCloudflareContext` example from changeset
dario-piotrowicz 1d76b5f
apply requested timout change
dario-piotrowicz e942a91
amend wrong timeout
dario-piotrowicz 8b8a25d
update process declaration in playwright configs
dario-piotrowicz 7bf0790
simplify middleware e2e check
dario-piotrowicz 3b1deb3
import type from "node:process"
dario-piotrowicz 9990519
update changeset
dario-piotrowicz a973ae5
Apply suggestions from code review
dario-piotrowicz 612f3f7
update changeset to minor
dario-piotrowicz 6473145
remove getting started info from README and redirect to official docs
dario-piotrowicz 7a272cb
Update examples/middleware/middleware.ts
dario-piotrowicz ed349ca
remove known issues section from readme
dario-piotrowicz 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 hidden or 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,28 @@ | ||
--- | ||
"@opennextjs/cloudflare": minor | ||
--- | ||
|
||
introduce new `initOpenNextCloudflareForDev` utility and make `getCloudflareContext` synchronous | ||
|
||
this change introduces a new `initOpenNextCloudflareForDev` function that must called in the [Next.js config file](https://nextjs.org/docs/app/api-reference/config/next-config-js) to integrate the Next.js dev server with the open-next Cloudflare adapter. | ||
|
||
Also makes `getCloudflareContext` synchronous. | ||
|
||
Additionally the `getCloudflareContext` can now work during local development (`next dev`) in the edge runtime (including middlewares). | ||
|
||
Moving forward we'll recommend that all applications include the use of the `initOpenNextCloudflareForDev` utility in their config file (there is no downside in doing so and it only effect local development). | ||
|
||
Example: | ||
|
||
```js | ||
// next.config.mjs | ||
|
||
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare"; | ||
|
||
initOpenNextCloudflareForDev(); | ||
|
||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = {}; | ||
|
||
export default nextConfig; | ||
``` |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,3 +1,25 @@ | ||
import { headers } from "next/headers"; | ||
|
||
export default function MiddlewarePage() { | ||
return <h1>Via middleware</h1>; | ||
const cloudflareContextHeader = headers().get("x-cloudflare-context"); | ||
|
||
return ( | ||
<> | ||
<h1>Via middleware</h1> | ||
<p> | ||
The value of the <i>x-cloudflare-context</i> header is: <br /> | ||
<span | ||
style={{ | ||
display: "inline-block", | ||
margin: "1rem 2rem", | ||
color: "grey", | ||
fontSize: "1.2rem", | ||
}} | ||
data-testid="cloudflare-context-header" | ||
> | ||
{cloudflareContextHeader} | ||
</span> | ||
</p> | ||
</> | ||
); | ||
} |
This file contains hidden or 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,9 @@ | ||
import { test, expect } from "@playwright/test"; | ||
|
||
test("middlewares have access to the cloudflare context", async ({ page }) => { | ||
await page.goto("/middleware"); | ||
const cloudflareContextHeaderElement = page.getByTestId("cloudflare-context-header"); | ||
expect(await cloudflareContextHeaderElement.textContent()).toContain( | ||
"typeof `cloudflareContext.env` = object" | ||
); | ||
}); |
This file contains hidden or 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 hidden or 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,54 @@ | ||
import { defineConfig, devices } from "@playwright/test"; | ||
import type nodeProcess from "node:process"; | ||
|
||
declare const process: typeof nodeProcess; | ||
|
||
/** | ||
* See https://playwright.dev/docs/test-configuration. | ||
*/ | ||
export default defineConfig({ | ||
testDir: "./", | ||
/* Run tests in files in parallel */ | ||
fullyParallel: true, | ||
/* Fail the build on CI if you accidentally left test.only in the source code. */ | ||
forbidOnly: !!process.env.CI, | ||
/* Retry on CI only */ | ||
retries: process.env.CI ? 2 : 0, | ||
/* Opt out of parallel tests on CI. */ | ||
workers: process.env.CI ? 1 : undefined, | ||
/* Reporter to use. See https://playwright.dev/docs/test-reporters */ | ||
reporter: "html", | ||
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ | ||
use: { | ||
/* Base URL to use in actions like `await page.goto('/')`. */ | ||
baseURL: "http://localhost:3334", | ||
|
||
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ | ||
trace: "on-first-retry", | ||
}, | ||
|
||
/* Configure projects for major browsers */ | ||
projects: [ | ||
{ | ||
name: "chromium", | ||
use: { ...devices["Desktop Chrome"] }, | ||
}, | ||
|
||
{ | ||
name: "firefox", | ||
use: { ...devices["Desktop Firefox"] }, | ||
}, | ||
|
||
{ | ||
name: "webkit", | ||
use: { ...devices["Desktop Safari"] }, | ||
}, | ||
], | ||
|
||
/* Run your local dev server before starting the tests */ | ||
webServer: { | ||
command: "pnpm dev --port 3334", | ||
url: "http://localhost:3334", | ||
reuseExistingServer: !process.env.CI, | ||
}, | ||
}); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.