Skip to content

Commit 522076b

Browse files
add "async mode" to getCloudflareContext (#372)
1 parent 54bcbc3 commit 522076b

19 files changed

+582
-65
lines changed

.changeset/tough-tables-talk.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
add "async mode" to `getCloudflareContext`
6+
7+
Add an `async` option to `getCloudflareContext({async})` to run it in "async mode", the difference being that the returned value is a
8+
promise of the Cloudflare context instead of the context itself
9+
10+
The main of this is that it allows the function to also run during SSG (since the missing context can be created on demand).

examples/common/apps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const apps = [
66
"playground",
77
"vercel-blog-starter",
88
"vercel-commerce",
9+
"ssg-app",
910
// e2e
1011
"app-pages-router",
1112
"app-router",

examples/ssg-app/.dev.vars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MY_SECRET = "psst... this is a secret!"

examples/ssg-app/.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.*
7+
.yarn/*
8+
!.yarn/patches
9+
!.yarn/plugins
10+
!.yarn/releases
11+
!.yarn/versions
12+
13+
# testing
14+
/coverage
15+
16+
# next.js
17+
/.next/
18+
/out/
19+
20+
# production
21+
/build
22+
23+
# misc
24+
.DS_Store
25+
*.pem
26+
27+
# debug
28+
npm-debug.log*
29+
yarn-debug.log*
30+
yarn-error.log*
31+
.pnpm-debug.log*
32+
33+
# env files (can opt-in for committing if needed)
34+
.env*
35+
36+
# vercel
37+
.vercel
38+
39+
# typescript
40+
*.tsbuildinfo
41+
next-env.d.ts
42+
43+
# playwright
44+
/test-results/
45+
/playwright-report/
46+
/blob-report/
47+
/playwright/.cache/

examples/ssg-app/app/favicon.ico

25.3 KB
Binary file not shown.

examples/ssg-app/app/globals.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
html,
2+
body {
3+
max-width: 100vw;
4+
overflow-x: hidden;
5+
height: 100vh;
6+
display: flex;
7+
flex-direction: column;
8+
}
9+
10+
footer {
11+
padding: 1rem;
12+
display: flex;
13+
justify-content: end;
14+
}

examples/ssg-app/app/layout.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { Metadata } from "next";
2+
import "./globals.css";
3+
4+
import { getCloudflareContext } from "@opennextjs/cloudflare";
5+
6+
export const metadata: Metadata = {
7+
title: "SSG App",
8+
description: "An app in which all the routes are SSG'd",
9+
};
10+
11+
export default async function RootLayout({
12+
children,
13+
}: Readonly<{
14+
children: React.ReactNode;
15+
}>) {
16+
const cloudflareContext = await getCloudflareContext({
17+
async: true,
18+
});
19+
20+
return (
21+
<html lang="en">
22+
<body>
23+
{children}
24+
<footer data-testid="app-version">{cloudflareContext.env.APP_VERSION}</footer>
25+
</body>
26+
</html>
27+
);
28+
}

examples/ssg-app/app/page.module.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.page {
2+
display: grid;
3+
grid-template-rows: 20px 1fr 20px;
4+
align-items: center;
5+
justify-items: center;
6+
flex: 1;
7+
border: 3px solid gray;
8+
margin: 1rem;
9+
margin-block-end: 0;
10+
}
11+
12+
.main {
13+
display: flex;
14+
flex-direction: column;
15+
gap: 32px;
16+
grid-row-start: 2;
17+
}

examples/ssg-app/app/page.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import styles from "./page.module.css";
2+
import { getCloudflareContext } from "@opennextjs/cloudflare";
3+
4+
export default async function Home() {
5+
const cloudflareContext = await getCloudflareContext({
6+
async: true,
7+
});
8+
9+
return (
10+
<div className={styles.page}>
11+
<main className={styles.main}>
12+
<h1>Hello from a Statically generated page</h1>
13+
<p data-testid="my-secret">{cloudflareContext.env.MY_SECRET}</p>
14+
</main>
15+
</div>
16+
);
17+
}

examples/ssg-app/e2e/base.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { test, expect } from "@playwright/test";
2+
3+
test("the index page should work", async ({ page }) => {
4+
await page.goto("/");
5+
await expect(page.getByText("Hello from a Statically generated page")).toBeVisible();
6+
});
7+
8+
test("the APP_VERSION var from the cloudflare context should be displayed", async ({ page }) => {
9+
await page.goto("/");
10+
await expect(page.getByTestId("app-version")).toHaveText("1.2.345");
11+
});
12+
13+
// Note: secrets from .dev.vars are also part of the SSG output, this is expected and nothing we can avoid
14+
test("the MY_SECRET secret from the cloudflare context should be displayed", async ({ page }) => {
15+
await page.goto("/");
16+
await expect(page.getByTestId("my-secret")).toHaveText("psst... this is a secret!");
17+
});

0 commit comments

Comments
 (0)