From 5dd1ed50b2f9428946b0b273e0ce8f13c19aa3b5 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 24 Oct 2023 18:26:34 +0800 Subject: [PATCH] Prevent Vercel serverless generate static redirect pages (#8896) --- .changeset/poor-steaks-confess.md | 5 ++++ .../vercel/src/serverless/adapter.ts | 1 + .../redirects-serverless/astro.config.mjs | 7 +++++ .../redirects-serverless/package.json | 9 ++++++ .../src/pages/index.astro | 8 ++++++ .../src/pages/subpage.astro | 0 .../vercel/test/redirects-serverless.test.js | 28 +++++++++++++++++++ pnpm-lock.yaml | 9 ++++++ 8 files changed, 67 insertions(+) create mode 100644 .changeset/poor-steaks-confess.md create mode 100644 packages/integrations/vercel/test/fixtures/redirects-serverless/astro.config.mjs create mode 100644 packages/integrations/vercel/test/fixtures/redirects-serverless/package.json create mode 100644 packages/integrations/vercel/test/fixtures/redirects-serverless/src/pages/index.astro create mode 100644 packages/integrations/vercel/test/fixtures/redirects-serverless/src/pages/subpage.astro create mode 100644 packages/integrations/vercel/test/redirects-serverless.test.js diff --git a/.changeset/poor-steaks-confess.md b/.changeset/poor-steaks-confess.md new file mode 100644 index 000000000000..254375c6ad41 --- /dev/null +++ b/.changeset/poor-steaks-confess.md @@ -0,0 +1,5 @@ +--- +'@astrojs/vercel': patch +--- + +Prevents the Vercel serverless adapter from generating static redirect pages in hybrid mode diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index e7cdebfb971c..ea7cd7e533f8 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -175,6 +175,7 @@ export default function vercelServerless({ serverEntry: 'entry.mjs', client: new URL('./static/', outDir), server: new URL('./dist/', config.root), + redirects: false, }, vite: { ...getSpeedInsightsViteConfig(speedInsights?.enabled || analytics), diff --git a/packages/integrations/vercel/test/fixtures/redirects-serverless/astro.config.mjs b/packages/integrations/vercel/test/fixtures/redirects-serverless/astro.config.mjs new file mode 100644 index 000000000000..37b08d2d7744 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/redirects-serverless/astro.config.mjs @@ -0,0 +1,7 @@ +import { defineConfig } from 'astro/config'; +import vercel from '@astrojs/vercel/serverless'; + +export default defineConfig({ + output: 'hybrid', + adapter: vercel(), +}); diff --git a/packages/integrations/vercel/test/fixtures/redirects-serverless/package.json b/packages/integrations/vercel/test/fixtures/redirects-serverless/package.json new file mode 100644 index 000000000000..c8de30d66c33 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/redirects-serverless/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/astro-vercel-redirects-serverless", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/vercel": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/integrations/vercel/test/fixtures/redirects-serverless/src/pages/index.astro b/packages/integrations/vercel/test/fixtures/redirects-serverless/src/pages/index.astro new file mode 100644 index 000000000000..9c077e2a381b --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/redirects-serverless/src/pages/index.astro @@ -0,0 +1,8 @@ + + + Testing + + +

Testing

+ + diff --git a/packages/integrations/vercel/test/fixtures/redirects-serverless/src/pages/subpage.astro b/packages/integrations/vercel/test/fixtures/redirects-serverless/src/pages/subpage.astro new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/integrations/vercel/test/redirects-serverless.test.js b/packages/integrations/vercel/test/redirects-serverless.test.js new file mode 100644 index 000000000000..f3a1a5daaf33 --- /dev/null +++ b/packages/integrations/vercel/test/redirects-serverless.test.js @@ -0,0 +1,28 @@ +import { expect } from 'chai'; +import { loadFixture } from './test-utils.js'; + +describe('Redirects Serverless', () => { + /** @type {import('astro/test/test-utils.js').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/redirects-serverless/', + redirects: { + '/one': '/', + '/other': '/subpage', + }, + }); + await fixture.build(); + }); + + it('does not create .html files', async () => { + let hasErrored = false; + try { + await fixture.readFile('../.vercel/output/static/other/index.html'); + } catch { + hasErrored = true; + } + expect(hasErrored).to.equal(true, 'this file should not exist'); + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bb4bbc5476ab..1940d296ab33 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4659,6 +4659,15 @@ importers: specifier: workspace:* version: link:../../../../../astro + packages/integrations/vercel/test/fixtures/redirects-serverless: + dependencies: + '@astrojs/vercel': + specifier: workspace:* + version: link:../../.. + astro: + specifier: workspace:* + version: link:../../../../../astro + packages/integrations/vercel/test/fixtures/serverless-prerender: dependencies: '@astrojs/vercel':