From 67ef6bdce7ca780e9f2cd49710e18d331b279e8b Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Thu, 9 May 2024 01:56:01 +0900 Subject: [PATCH 1/4] feat(cloudflare-pages): custom `_routes.json` --- .../cloudflare-pages/src/cloudflare-pages.ts | 42 +++++++++++-------- .../test/cloudflare-pages.test.ts | 22 ++++++++++ .../project/public-routes-json/_routes.json | 1 + 3 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 packages/cloudflare-pages/test/project/public-routes-json/_routes.json diff --git a/packages/cloudflare-pages/src/cloudflare-pages.ts b/packages/cloudflare-pages/src/cloudflare-pages.ts index 2179213..a5a79bf 100644 --- a/packages/cloudflare-pages/src/cloudflare-pages.ts +++ b/packages/cloudflare-pages/src/cloudflare-pages.ts @@ -30,6 +30,7 @@ export const defaultOptions: Required { - if (p.isDirectory()) { - staticPaths.push(`/${p.name}/*`) - } else { - if (p.name === WORKER_JS_NAME) { - return + // If _routes.json already exists, don't create it + if (paths.some((p) => p.name === ROUTES_JSON_NAME)) { + return + } else { + paths.forEach((p) => { + if (p.isDirectory()) { + staticPaths.push(`/${p.name}/*`) + } else { + if (p.name === WORKER_JS_NAME) { + return + } + staticPaths.push(`/${p.name}`) } - staticPaths.push(`/${p.name}`) + }) + const staticRoutes: StaticRoutes = { + version: 1, + include: ['/*'], + exclude: staticPaths, } - }) - const staticRoutes: StaticRoutes = { - version: 1, - include: ['/*'], - exclude: staticPaths, + const path = resolve( + config.root, + options?.outputDir ?? defaultOptions.outputDir, + '_routes.json' + ) + await writeFile(path, JSON.stringify(staticRoutes)) } - const path = resolve( - config.root, - options?.outputDir ?? defaultOptions.outputDir, - '_routes.json' - ) - await writeFile(path, JSON.stringify(staticRoutes)) }, config: async (): Promise => { return { diff --git a/packages/cloudflare-pages/test/cloudflare-pages.test.ts b/packages/cloudflare-pages/test/cloudflare-pages.test.ts index 06b8a36..50d02d8 100644 --- a/packages/cloudflare-pages/test/cloudflare-pages.test.ts +++ b/packages/cloudflare-pages/test/cloudflare-pages.test.ts @@ -67,4 +67,26 @@ describe('cloudflarePagesPlugin', () => { '{"version":1,"include":["/*"],"exclude":["/favicon.ico","/static/*"]}' ) }) + + it('Should not create a new _routes.json when _routes.json on output directory.', async () => { + const outputFile = `${testDir}/dist/_worker.js` + const routesFile = `${testDir}/dist/_routes.json` + + expect(fs.existsSync(entryFile)).toBe(true) + + await build({ + publicDir: 'public-routes-json', + root: testDir, + plugins: [cloudflarePagesPlugin()], + }) + + expect(fs.existsSync(outputFile)).toBe(true) + expect(fs.existsSync(routesFile)).toBe(true) + + const output = fs.readFileSync(outputFile, 'utf-8') + expect(output).toContain('Hello World') + + const routes = fs.readFileSync(routesFile, 'utf-8') + expect(routes).toContain('{"version":1,"include":["/"],"exclude":["Custom Routes"]}') + }) }) diff --git a/packages/cloudflare-pages/test/project/public-routes-json/_routes.json b/packages/cloudflare-pages/test/project/public-routes-json/_routes.json new file mode 100644 index 0000000..3317a71 --- /dev/null +++ b/packages/cloudflare-pages/test/project/public-routes-json/_routes.json @@ -0,0 +1 @@ +{"version":1,"include":["/"],"exclude":["Custom Routes"]} \ No newline at end of file From c04b29e238fc478dab307dba86f8c0a0978f2bf8 Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Thu, 9 May 2024 19:59:44 +0900 Subject: [PATCH 2/4] change path name on exclude like an actual path --- packages/cloudflare-pages/test/cloudflare-pages.test.ts | 2 +- .../test/project/public-routes-json/_routes.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cloudflare-pages/test/cloudflare-pages.test.ts b/packages/cloudflare-pages/test/cloudflare-pages.test.ts index 50d02d8..3147ce4 100644 --- a/packages/cloudflare-pages/test/cloudflare-pages.test.ts +++ b/packages/cloudflare-pages/test/cloudflare-pages.test.ts @@ -87,6 +87,6 @@ describe('cloudflarePagesPlugin', () => { expect(output).toContain('Hello World') const routes = fs.readFileSync(routesFile, 'utf-8') - expect(routes).toContain('{"version":1,"include":["/"],"exclude":["Custom Routes"]}') + expect(routes).toContain('{"version":1,"include":["/"],"exclude":["/customRoute"]}') }) }) diff --git a/packages/cloudflare-pages/test/project/public-routes-json/_routes.json b/packages/cloudflare-pages/test/project/public-routes-json/_routes.json index 3317a71..2a43272 100644 --- a/packages/cloudflare-pages/test/project/public-routes-json/_routes.json +++ b/packages/cloudflare-pages/test/project/public-routes-json/_routes.json @@ -1 +1 @@ -{"version":1,"include":["/"],"exclude":["Custom Routes"]} \ No newline at end of file +{"version":1,"include":["/"],"exclude":["/customRoute"]} \ No newline at end of file From 3b518ad3c2083c6fbc1ee46317bdd5e2629b1f1f Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Thu, 9 May 2024 20:01:07 +0900 Subject: [PATCH 3/4] add changeset --- .changeset/moody-islands-wave.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/moody-islands-wave.md diff --git a/.changeset/moody-islands-wave.md b/.changeset/moody-islands-wave.md new file mode 100644 index 0000000..423664e --- /dev/null +++ b/.changeset/moody-islands-wave.md @@ -0,0 +1,5 @@ +--- +"@hono/vite-cloudflare-pages": minor +--- + +feat: creating a `public/_routes.json` will override the automatic generation From 2613ca90a02e788c5bf70e556d3b16d6164c4fde Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Thu, 9 May 2024 20:19:55 +0900 Subject: [PATCH 4/4] add description to README for custom `_routes.json` --- packages/cloudflare-pages/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/cloudflare-pages/README.md b/packages/cloudflare-pages/README.md index 7ba7202..fe9c937 100644 --- a/packages/cloudflare-pages/README.md +++ b/packages/cloudflare-pages/README.md @@ -79,6 +79,8 @@ export const defaultOptions = { } ``` +This plugin generates `_routes.json` automatically. The automatic generation can be overridden by creating a `public/_routes.json`. See [Create a `_routes.json` file](https://developers.cloudflare.com/pages/functions/routing/#create-a-_routesjson-file) on Cloudflare Docs for more details. + ## Build a client If you also want to build a client-side script, you can configure it as follows.