Skip to content

Commit 1261c0c

Browse files
Support local assets (#22)
* Update create-next-app page.tsx to match latest * fix: add support for serving static assets in the `public` directory See https://nextjs.org/docs/pages/building-your-application/optimizing/static-assets
1 parent 4fadf97 commit 1261c0c

File tree

5 files changed

+94
-66
lines changed

5 files changed

+94
-66
lines changed
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

examples/create-next-app/src/app/page.tsx

Lines changed: 80 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,110 @@ import Image from "next/image";
22

33
export default function Home() {
44
return (
5-
<div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]">
6-
<main className="flex flex-col gap-8 row-start-2 items-center sm:items-start">
7-
<Image
8-
className="dark:invert"
9-
src="https://nextjs.org/icons/next.svg"
10-
alt="Next.js logo"
11-
width={180}
12-
height={38}
13-
priority
14-
/>
15-
<ol className="list-inside list-decimal text-sm text-center sm:text-left font-[family-name:var(--font-geist-mono)]">
16-
<li className="mb-2">
17-
Get started by editing{" "}
18-
<code className="bg-black/[.05] dark:bg-white/[.06] px-1 py-0.5 rounded font-semibold">
19-
src/app/page.tsx
20-
</code>
21-
.
22-
</li>
23-
<li>Save and see your changes instantly.</li>
24-
</ol>
25-
26-
<div className="flex gap-4 items-center flex-col sm:flex-row">
5+
<main className="flex min-h-screen flex-col items-center justify-between p-24">
6+
<div className="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex">
7+
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
8+
Get started by editing&nbsp;
9+
<code className="font-mono font-bold">src/app/page.tsx</code>
10+
</p>
11+
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:size-auto lg:bg-none">
2712
<a
28-
className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5"
29-
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
13+
className="pointer-events-none flex place-items-center gap-2 p-8 lg:pointer-events-auto lg:p-0"
14+
href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
3015
target="_blank"
3116
rel="noopener noreferrer"
3217
>
18+
By{" "}
3319
<Image
20+
src="/vercel.svg"
21+
alt="Vercel Logo"
3422
className="dark:invert"
35-
src="https://nextjs.org/icons/vercel.svg"
36-
alt="Vercel logomark"
37-
width={20}
38-
height={20}
23+
width={100}
24+
height={24}
25+
priority
3926
/>
40-
Deploy now
41-
</a>
42-
<a
43-
className="rounded-full border border-solid border-black/[.08] dark:border-white/[.145] transition-colors flex items-center justify-center hover:bg-[#f2f2f2] dark:hover:bg-[#1a1a1a] hover:border-transparent text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 sm:min-w-44"
44-
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
45-
target="_blank"
46-
rel="noopener noreferrer"
47-
>
48-
Read our docs
4927
</a>
5028
</div>
51-
</main>
52-
<footer className="row-start-3 flex gap-6 flex-wrap items-center justify-center">
29+
</div>
30+
31+
<div className="relative z-[-1] flex place-items-center before:absolute before:h-[300px] before:w-full before:-translate-x-1/2 before:rounded-full before:bg-gradient-radial before:from-white before:to-transparent before:blur-2xl before:content-[''] after:absolute after:-z-20 after:h-[180px] after:w-full after:translate-x-1/3 after:bg-gradient-conic after:from-sky-200 after:via-blue-200 after:blur-2xl after:content-[''] before:dark:bg-gradient-to-br before:dark:from-transparent before:dark:to-blue-700 before:dark:opacity-10 after:dark:from-sky-900 after:dark:via-[#0141ff] after:dark:opacity-40 sm:before:w-[480px] sm:after:w-[240px] before:lg:h-[360px]">
32+
<Image
33+
className="relative dark:drop-shadow-[0_0_0.3rem_#ffffff70] dark:invert"
34+
src="/next.svg"
35+
alt="Next.js Logo"
36+
width={180}
37+
height={37}
38+
priority
39+
/>
40+
</div>
41+
42+
<div className="mb-32 grid text-center lg:mb-0 lg:w-full lg:max-w-5xl lg:grid-cols-4 lg:text-left">
43+
<a
44+
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
45+
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
46+
target="_blank"
47+
rel="noopener noreferrer"
48+
>
49+
<h2 className="mb-3 text-2xl font-semibold">
50+
Docs{" "}
51+
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
52+
-&gt;
53+
</span>
54+
</h2>
55+
<p className="m-0 max-w-[30ch] text-sm opacity-50">
56+
Find in-depth information about Next.js features and API.
57+
</p>
58+
</a>
59+
5360
<a
54-
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
5561
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
62+
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
5663
target="_blank"
5764
rel="noopener noreferrer"
5865
>
59-
<Image aria-hidden src="https://nextjs.org/icons/file.svg" alt="File icon" width={16} height={16} />
60-
Learn
66+
<h2 className="mb-3 text-2xl font-semibold">
67+
Learn{" "}
68+
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
69+
-&gt;
70+
</span>
71+
</h2>
72+
<p className="m-0 max-w-[30ch] text-sm opacity-50">
73+
Learn about Next.js in an interactive course with&nbsp;quizzes!
74+
</p>
6175
</a>
76+
6277
<a
63-
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
64-
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
78+
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
79+
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
6580
target="_blank"
6681
rel="noopener noreferrer"
6782
>
68-
<Image
69-
aria-hidden
70-
src="https://nextjs.org/icons/window.svg"
71-
alt="Window icon"
72-
width={16}
73-
height={16}
74-
/>
75-
Examples
83+
<h2 className="mb-3 text-2xl font-semibold">
84+
Templates{" "}
85+
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
86+
-&gt;
87+
</span>
88+
</h2>
89+
<p className="m-0 max-w-[30ch] text-sm opacity-50">Explore starter templates for Next.js.</p>
7690
</a>
91+
7792
<a
78-
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
79-
href="https://nextjs.org?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
93+
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
94+
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
8095
target="_blank"
8196
rel="noopener noreferrer"
8297
>
83-
<Image
84-
aria-hidden
85-
src="https://nextjs.org/icons/globe.svg"
86-
alt="Globe icon"
87-
width={16}
88-
height={16}
89-
/>
90-
Go to nextjs.org →
98+
<h2 className="mb-3 text-2xl font-semibold">
99+
Deploy{" "}
100+
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
101+
-&gt;
102+
</span>
103+
</h2>
104+
<p className="m-0 max-w-[30ch] text-balance text-sm opacity-50">
105+
Instantly deploy your Next.js site to a shareable URL with Vercel.
106+
</p>
91107
</a>
92-
</footer>
93-
</div>
108+
</div>
109+
</main>
94110
);
95111
}

packages/cloudflare/src/build/build-worker.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextjsAppPaths } from "../nextjs-paths";
22
import { build, Plugin } from "esbuild";
3-
import { readFileSync } from "node:fs";
3+
import { existsSync, readFileSync } from "node:fs";
44
import { cp, readFile, writeFile } from "node:fs/promises";
55

66
import { patchRequire } from "./patches/investigated/patch-require";
@@ -20,6 +20,7 @@ import { updateWebpackChunksFile } from "./patches/investigated/update-webpack-c
2020
* @param nextjsAppPaths
2121
*/
2222
export async function buildWorker(
23+
inputNextAppDir: string,
2324
outputDir: string,
2425
nextjsAppPaths: NextjsAppPaths,
2526
templateSrcDir: string
@@ -124,10 +125,19 @@ Request = globalThis.Request;
124125
await updateWorkerBundledCode(workerOutputFile, nextjsAppPaths);
125126

126127
console.log(`\x1b[35m⚙️ Copying asset files...\n\x1b[0m`);
128+
129+
// Copy over client-side generated files
127130
await cp(`${nextjsAppPaths.dotNextDir}/static`, `${outputDir}/assets/_next/static`, {
128131
recursive: true,
129132
});
130133

134+
// Copy over any static files (e.g. images) from the source project
135+
if (existsSync(`${inputNextAppDir}/public`)) {
136+
await cp(`${inputNextAppDir}/public`, `${outputDir}/assets`, {
137+
recursive: true,
138+
});
139+
}
140+
131141
console.log(`\x1b[35mWorker saved in \`${workerOutputFile}\` 🚀\n\x1b[0m`);
132142
}
133143

packages/cloudflare/src/build/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export async function build(inputNextAppDir: string, opts: BuildOptions): Promis
4343

4444
const templateDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "templates");
4545

46-
await buildWorker(outputDir, nextjsAppPaths, templateDir);
46+
await buildWorker(inputNextAppDir, outputDir, nextjsAppPaths, templateDir);
4747
}
4848

4949
type BuildOptions = {

0 commit comments

Comments
 (0)