-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more efficient _routes.json for cloudflare integration (#7846)
* more efficient _routes.json for cloudflare integration * added tests * updated pnpm-lock.yaml * added changeset * cleaned up test * fix: convert window path separators * updated docs * handle more cases * Apply suggestions from code review Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * incorporate feedback from code review * used other pnpm version * better fallback for empty include array * adjust test case to changed fallback for empty include array * updated docs --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
- Loading branch information
Showing
17 changed files
with
242 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/cloudflare': minor | ||
--- | ||
|
||
More efficient \_routes.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/integrations/cloudflare/test/fixtures/routesJson/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import cloudflare from '@astrojs/cloudflare'; | ||
|
||
export default defineConfig({ | ||
adapter: cloudflare({ mode: 'directory' }), | ||
output: 'hybrid', | ||
redirects: { | ||
'/a/redirect': '/', | ||
}, | ||
srcDir: process.env.SRC | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/integrations/cloudflare/test/fixtures/routesJson/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "@test/astro-cloudflare-routes-json", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"@astrojs/cloudflare": "workspace:*", | ||
"astro": "workspace:*" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ages/integrations/cloudflare/test/fixtures/routesJson/src/dynamicOnly/pages/another.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
export const prerender=false; | ||
--- | ||
|
||
ok |
5 changes: 5 additions & 0 deletions
5
packages/integrations/cloudflare/test/fixtures/routesJson/src/dynamicOnly/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
export const prerender=false; | ||
--- | ||
|
||
ok |
5 changes: 5 additions & 0 deletions
5
packages/integrations/cloudflare/test/fixtures/routesJson/src/mixed/pages/a/[...rest].astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
export const prerender=false; | ||
--- | ||
|
||
ok |
5 changes: 5 additions & 0 deletions
5
packages/integrations/cloudflare/test/fixtures/routesJson/src/mixed/pages/a/[id].astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
export const prerender=false; | ||
--- | ||
|
||
ok |
1 change: 1 addition & 0 deletions
1
packages/integrations/cloudflare/test/fixtures/routesJson/src/mixed/pages/a/endpoint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const prerender = false; |
1 change: 1 addition & 0 deletions
1
packages/integrations/cloudflare/test/fixtures/routesJson/src/mixed/pages/a/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ok |
1 change: 1 addition & 0 deletions
1
packages/integrations/cloudflare/test/fixtures/routesJson/src/mixed/pages/b/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ok |
1 change: 1 addition & 0 deletions
1
packages/integrations/cloudflare/test/fixtures/routesJson/src/mixed/public/public.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ok |
1 change: 1 addition & 0 deletions
1
packages/integrations/cloudflare/test/fixtures/routesJson/src/staticOnly/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { expect } from 'chai'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
/** @type {import('./test-utils.js').Fixture} */ | ||
describe('_routes.json generation', () => { | ||
after(() => { | ||
delete process.env.SRC; | ||
}); | ||
|
||
describe('of both functions and static files', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
process.env.SRC = './src/mixed'; | ||
fixture = await loadFixture({ | ||
root: './fixtures/routesJson/', | ||
}); | ||
await fixture.build(); | ||
}); | ||
|
||
it('creates `include` for functions and `exclude` for static files where needed', async () => { | ||
const _routesJson = await fixture.readFile('/_routes.json'); | ||
const routes = JSON.parse(_routesJson); | ||
|
||
expect(routes).to.deep.equal({ | ||
version: 1, | ||
include: ['/a/*'], | ||
exclude: ['/a/', '/a/redirect', '/a/index.html'], | ||
}); | ||
}); | ||
}); | ||
|
||
describe('of only functions', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
process.env.SRC = './src/dynamicOnly'; | ||
fixture = await loadFixture({ | ||
root: './fixtures/routesJson/', | ||
}); | ||
await fixture.build(); | ||
}); | ||
|
||
it('creates a wildcard `include` and `exclude` only for the redirect', async () => { | ||
const _routesJson = await fixture.readFile('/_routes.json'); | ||
const routes = JSON.parse(_routesJson); | ||
|
||
expect(routes).to.deep.equal({ | ||
version: 1, | ||
include: ['/*'], | ||
exclude: ['/a/redirect'], | ||
}); | ||
}); | ||
}); | ||
|
||
describe('of only static files', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
process.env.SRC = './src/staticOnly'; | ||
fixture = await loadFixture({ | ||
root: './fixtures/routesJson/', | ||
}); | ||
await fixture.build(); | ||
}); | ||
|
||
it('create only one `include` and `exclude` that are supposed to match nothing', async () => { | ||
const _routesJson = await fixture.readFile('/_routes.json'); | ||
const routes = JSON.parse(_routesJson); | ||
|
||
expect(routes).to.deep.equal({ | ||
version: 1, | ||
include: ['/'], | ||
exclude: ['/'], | ||
}); | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.