Skip to content

Commit

Permalink
feat(cloudflare-pages): custom _routes.json (#135)
Browse files Browse the repository at this point in the history
* feat(cloudflare-pages): custom `_routes.json`

* change path name on exclude

like an actual path

* add changeset

* add description to README for custom `_routes.json`
  • Loading branch information
ryuapp authored May 9, 2024
1 parent dd57f89 commit 25bfb61
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-islands-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hono/vite-cloudflare-pages": minor
---

feat: creating a `public/_routes.json` will override the automatic generation
2 changes: 2 additions & 0 deletions packages/cloudflare-pages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 24 additions & 18 deletions packages/cloudflare-pages/src/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const defaultOptions: Required<Omit<CloudflarePagesOptions, 'serveStaticD
}

const WORKER_JS_NAME = '_worker.js'
const ROUTES_JSON_NAME = '_routes.json'

type StaticRoutes = { version: number; include: string[]; exclude: string[] }

Expand Down Expand Up @@ -64,27 +65,32 @@ export const cloudflarePagesPlugin = (options?: CloudflarePagesOptions): Plugin
const paths = await readdir(resolve(config.root, config.build.outDir), {
withFileTypes: true,
})
paths.forEach((p) => {
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<UserConfig> => {
return {
Expand Down
22 changes: 22 additions & 0 deletions packages/cloudflare-pages/test/cloudflare-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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":["/customRoute"]}')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"include":["/"],"exclude":["/customRoute"]}

0 comments on commit 25bfb61

Please sign in to comment.