Skip to content

Commit

Permalink
feat(cloudflare-pages): custom _routes.json
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuapp committed May 8, 2024
1 parent f2ee720 commit 67ef6bd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
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":["Custom Routes"]}')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"include":["/"],"exclude":["Custom Routes"]}

0 comments on commit 67ef6bd

Please sign in to comment.