diff options
author | 2023-06-05 09:03:20 -0400 | |
---|---|---|
committer | 2023-06-05 09:03:20 -0400 | |
commit | 8f42efaeccdcf883516b13fe7039ec8634ed20b3 (patch) | |
tree | 5a074d594ff034dcf99b9a018cfc958d6053dddb | |
parent | 77e30465aeb97f30f06d94b685cf371d7d3c8ec5 (diff) | |
download | astro-8f42efaeccdcf883516b13fe7039ec8634ed20b3.tar.gz astro-8f42efaeccdcf883516b13fe7039ec8634ed20b3.tar.zst astro-8f42efaeccdcf883516b13fe7039ec8634ed20b3.zip |
Redirects (#7067)
* Redirects spike
* Allow redirects in static mode
* Support in Netlify as well
* Adding a changeset
* Rename file
* Fix build problem
* Refactor to be more modular
* Fix location ref
* Late test should only run in SSR
* Support redirects in Netlify SSR configuration (#7167)
* Implement support for dynamic routes in redirects (#7173)
* Implement support for dynamic routes in redirects
* Remove the .only
* No need to special-case redirects in static build
* Implement support for redirects config in the Vercel adapter (#7182)
* Implement support for redirects config in the Vercel adapter
* Remove unused condition
* Move to a internal helper package
* Add support for the object notation in redirects
* Use status 308 for non-GET redirects (#7186)
* Implement redirects in Cloudflare (#7198)
* Implement redirects in Cloudflare
* Fix build
* Update tests b/c of new ordering
* Debug issue
* Use posix.join
* Update packages/underscore-redirects/package.json
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
* Update based on review comments
* Update broken test
---------
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
* Test that redirects can come from middleware (#7213)
* Test that redirects can come from middleware
* Allow non-promise returns for middleware
* Implement priority (#7210)
* Refactor
* Fix netlify test ordering
* Fix ordering again
* Redirects: Allow preventing the output of the static HTML file (#7245)
* Do a simple push for priority
* Adding changesets
* Put the implementation behind a flag.
* Self review
* Update .changeset/chatty-actors-stare.md
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
* Update packages/astro/src/@types/astro.ts
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
* Update packages/astro/src/@types/astro.ts
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
* Update packages/astro/src/@types/astro.ts
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
* Update packages/astro/src/@types/astro.ts
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
* Update docs on dynamic restrictions.
* Update packages/astro/src/@types/astro.ts
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Update packages/astro/src/@types/astro.ts
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Code review changes
* Document netlify static adapter
* Update packages/astro/src/@types/astro.ts
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Slight reword
* Update .changeset/twenty-suns-vanish.md
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Add a note about public/_redirects file
* Update packages/astro/src/@types/astro.ts
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
---------
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
-rw-r--r-- | packages/integrations/cloudflare/package.json | 1 | ||||
-rw-r--r-- | packages/integrations/cloudflare/src/index.ts | 17 | ||||
-rw-r--r-- | packages/integrations/cloudflare/test/directory.test.js | 18 |
3 files changed, 35 insertions, 1 deletions
diff --git a/packages/integrations/cloudflare/package.json b/packages/integrations/cloudflare/package.json index b7add3177..b6f2caef2 100644 --- a/packages/integrations/cloudflare/package.json +++ b/packages/integrations/cloudflare/package.json @@ -38,6 +38,7 @@ "test": "mocha --exit --timeout 30000 test/" }, "dependencies": { + "@astrojs/underscore-redirects": "^0.1.0", "esbuild": "^0.17.12", "tiny-glob": "^0.2.9" }, diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 2f6b36e87..ca755432e 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -1,4 +1,5 @@ import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro'; +import { createRedirectsFromAstroRoutes, type Redirects } from '@astrojs/underscore-redirects'; import esbuild from 'esbuild'; import * as fs from 'fs'; import * as os from 'os'; @@ -50,6 +51,7 @@ export default function createIntegration(args?: Options): AstroIntegration { client: new URL(`.${config.base}`, config.outDir), server: new URL(`.${SERVER_BUILD_FOLDER}`, config.outDir), serverEntry: '_worker.mjs', + redirects: false, }, }); }, @@ -88,7 +90,7 @@ export default function createIntegration(args?: Options): AstroIntegration { vite.ssr.target = 'webworker'; } }, - 'astro:build:done': async ({ pages }) => { + 'astro:build:done': async ({ pages, routes, dir }) => { const entryPath = fileURLToPath(new URL(_buildConfig.serverEntry, _buildConfig.server)); const entryUrl = new URL(_buildConfig.serverEntry, _config.outDir); const buildPath = fileURLToPath(entryUrl); @@ -197,6 +199,19 @@ export default function createIntegration(args?: Options): AstroIntegration { } } + const redirectRoutes = routes.filter(r => r.type === 'redirect'); + const trueRedirects = createRedirectsFromAstroRoutes({ + config: _config, + routes: redirectRoutes, + dir, + }); + if(!trueRedirects.empty()) { + await fs.promises.appendFile( + new URL('./_redirects', _config.outDir), + trueRedirects.print() + ); + } + await fs.promises.writeFile( new URL('./_routes.json', _config.outDir), JSON.stringify( diff --git a/packages/integrations/cloudflare/test/directory.test.js b/packages/integrations/cloudflare/test/directory.test.js index 67693310a..e88019401 100644 --- a/packages/integrations/cloudflare/test/directory.test.js +++ b/packages/integrations/cloudflare/test/directory.test.js @@ -11,6 +11,12 @@ describe('mode: "directory"', () => { root: './fixtures/basics/', output: 'server', adapter: cloudflare({ mode: 'directory' }), + redirects: { + '/old': '/' + }, + experimental: { + redirects: true, + }, }); await fixture.build(); }); @@ -19,4 +25,16 @@ describe('mode: "directory"', () => { expect(await fixture.pathExists('../functions')).to.be.true; expect(await fixture.pathExists('../functions/[[path]].js')).to.be.true; }); + + it('generates a redirects file', async () => { + try { + let _redirects = await fixture.readFile('/_redirects'); + let parts = _redirects.split(/\s+/); + expect(parts).to.deep.equal([ + '/old', '/', '301' + ]); + } catch { + expect(false).to.equal(true); + } + }); }); |