diff options
author | 2023-06-05 09:03:20 -0400 | |
---|---|---|
committer | 2023-06-05 09:03:20 -0400 | |
commit | 57f8d14c027c30919363e12c664ccff4ed64d0fc (patch) | |
tree | 61817073af197b716af5f0d5b55a6bac34abecdc /packages/integrations/netlify/test | |
parent | dd1a6b6c941aeb7af934bd12db22412af262f5a1 (diff) | |
download | astro-57f8d14c027c30919363e12c664ccff4ed64d0fc.tar.gz astro-57f8d14c027c30919363e12c664ccff4ed64d0fc.tar.zst astro-57f8d14c027c30919363e12c664ccff4ed64d0fc.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>
Diffstat (limited to 'packages/integrations/netlify/test')
6 files changed, 150 insertions, 0 deletions
diff --git a/packages/integrations/netlify/test/functions/redirects.test.js b/packages/integrations/netlify/test/functions/redirects.test.js new file mode 100644 index 000000000..8a6d36694 --- /dev/null +++ b/packages/integrations/netlify/test/functions/redirects.test.js @@ -0,0 +1,44 @@ +import { expect } from 'chai'; +import { load as cheerioLoad } from 'cheerio'; +import { loadFixture, testIntegration } from './test-utils.js'; +import netlifyAdapter from '../../dist/index.js'; +import { fileURLToPath } from 'url'; + +describe('SSG - Redirects', () => { + /** @type {import('../../../astro/test/test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('../static/fixtures/redirects/', import.meta.url).toString(), + output: 'server', + adapter: netlifyAdapter({ + dist: new URL('../static/fixtures/redirects/dist/', import.meta.url), + }), + site: `http://example.com`, + integrations: [testIntegration()], + redirects: { + '/other': '/' + }, + experimental: { + redirects: true, + }, + }); + await fixture.build(); + }); + + it('Creates a redirects file', async () => { + let redirects = await fixture.readFile('/_redirects'); + let parts = redirects.split(/\s+/); + expect(parts).to.deep.equal([ + '/other', '/', '301', + // This uses the dynamic Astro.redirect, so we don't know that it's a redirect + // until runtime. This is correct! + '/nope', '/.netlify/functions/entry', '200', + '/', '/.netlify/functions/entry', '200', + + // A real route + '/team/articles/*', '/.netlify/functions/entry', '200', + ]); + }); +}); diff --git a/packages/integrations/netlify/test/static/fixtures/redirects/src/pages/index.astro b/packages/integrations/netlify/test/static/fixtures/redirects/src/pages/index.astro new file mode 100644 index 000000000..53e029f04 --- /dev/null +++ b/packages/integrations/netlify/test/static/fixtures/redirects/src/pages/index.astro @@ -0,0 +1,6 @@ +<html> +<head><title>Testing</title></head> +<body> + <h1>Testing</h1> +</body> +</html> diff --git a/packages/integrations/netlify/test/static/fixtures/redirects/src/pages/nope.astro b/packages/integrations/netlify/test/static/fixtures/redirects/src/pages/nope.astro new file mode 100644 index 000000000..f48d767ee --- /dev/null +++ b/packages/integrations/netlify/test/static/fixtures/redirects/src/pages/nope.astro @@ -0,0 +1,3 @@ +--- +return Astro.redirect('/'); +--- diff --git a/packages/integrations/netlify/test/static/fixtures/redirects/src/pages/team/articles/[...slug].astro b/packages/integrations/netlify/test/static/fixtures/redirects/src/pages/team/articles/[...slug].astro new file mode 100644 index 000000000..716d3bd5d --- /dev/null +++ b/packages/integrations/netlify/test/static/fixtures/redirects/src/pages/team/articles/[...slug].astro @@ -0,0 +1,25 @@ +--- +export const getStaticPaths = (async () => { + const posts = [ + { slug: 'one', data: {draft: false, title: 'One'} }, + { slug: 'two', data: {draft: false, title: 'Two'} } + ]; + return posts.map((post) => { + return { + params: { slug: post.slug }, + props: { draft: post.data.draft, title: post.data.title }, + }; + }); +}) + +const { slug } = Astro.params; +const { title } = Astro.props; +--- +<html> + <head> + <title>{ title }</title> + </head> + <body> + <h1>{ title }</h1> + </body> +</html> diff --git a/packages/integrations/netlify/test/static/redirects.test.js b/packages/integrations/netlify/test/static/redirects.test.js new file mode 100644 index 000000000..0b153b31c --- /dev/null +++ b/packages/integrations/netlify/test/static/redirects.test.js @@ -0,0 +1,43 @@ +import { expect } from 'chai'; +import { loadFixture, testIntegration } from './test-utils.js'; +import { netlifyStatic } from '../../dist/index.js'; + +describe('SSG - Redirects', () => { + /** @type {import('../../../astro/test/test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('./fixtures/redirects/', import.meta.url).toString(), + output: 'static', + adapter: netlifyStatic(), + experimental: { + redirects: true, + }, + site: `http://example.com`, + integrations: [testIntegration()], + redirects: { + '/other': '/', + '/two': { + status: 302, + destination: '/' + }, + '/blog/[...slug]': '/team/articles/[...slug]' + } + }); + await fixture.build(); + }); + + it('Creates a redirects file', async () => { + let redirects = await fixture.readFile('/_redirects'); + let parts = redirects.split(/\s+/); + expect(parts).to.deep.equal([ + '/two', '/', '302', + '/other', '/', '301', + '/nope', '/', '301', + + '/blog/*', '/team/articles/*/index.html', '301', + '/team/articles/*', '/team/articles/*/index.html', '200', + ]); + }); +}); diff --git a/packages/integrations/netlify/test/static/test-utils.js b/packages/integrations/netlify/test/static/test-utils.js new file mode 100644 index 000000000..02b5d2ad9 --- /dev/null +++ b/packages/integrations/netlify/test/static/test-utils.js @@ -0,0 +1,29 @@ +// @ts-check +import { fileURLToPath } from 'url'; + +export * from '../../../../astro/test/test-utils.js'; + +/** + * + * @returns {import('../../../../astro/dist/types/@types/astro').AstroIntegration} + */ +export function testIntegration() { + return { + name: '@astrojs/netlify/test-integration', + hooks: { + 'astro:config:setup': ({ updateConfig }) => { + updateConfig({ + vite: { + resolve: { + alias: { + '@astrojs/netlify/netlify-functions.js': fileURLToPath( + new URL('../../dist/netlify-functions.js', import.meta.url) + ), + }, + }, + }, + }); + }, + }, + }; +} |