Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/five-kids-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@astrojs/cloudflare': patch
---
Fixes unnecessary prerendering of redirect destinations

Unnecessary files are no longer generated by static builds for redirected routes.

Requests are no longer made at build time to external redirect destination URLs, which could cause builds to fail.
1 change: 1 addition & 0 deletions packages/integrations/cloudflare/src/prerenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export function createCloudflarePrerenderer({
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
redirect: 'manual',
});

return response;
Expand Down
37 changes: 37 additions & 0 deletions packages/integrations/cloudflare/test/external-redirects.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it } from 'node:test';
import { loadFixture } from './_test-utils.js';
import assert from 'node:assert/strict';
import { existsSync, readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';

describe('External Redirects', () => {
let fixture;

it('should not attempt to prerender external redirect destinations', async () => {
fixture = await loadFixture({
root: './fixtures/external-redirects',
});

// Building should not result in a fetch to the external destination URL.
// If it does, the fetch will throw and the test will fail.
await fixture.build();

// Check that the redirect file was created and contains the redirect
const redirectsPath = fileURLToPath(new URL('client/_redirects', fixture.config.outDir));
const redirectsContent = readFileSync(redirectsPath, 'utf-8');
assert.match(
redirectsContent,
/\/redirect\s+http:\/\/test.invalid\/destination\s+301/,
'_redirects file should contain the redirect rule',
);

// Check that the destination was not prerendered
const prerenderedPath = fileURLToPath(
new URL('client/redirect/index.html', fixture.config.outDir),
);
assert.ok(
!existsSync(prerenderedPath),
'Should not create prerendered file for external redirect destination',
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from "astro/config";
import cloudflare from "@astrojs/cloudflare";

export default defineConfig({
adapter: cloudflare(),
output: 'static',
redirects: {
"/redirect": "http://test.invalid/destination",
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@test/astro-cloudflare-external-redirects",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "astro build"
},
"dependencies": {
"@astrojs/cloudflare": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
---
<html>
<head>
<title>External Redirects Test</title>
</head>
<body>
<h1>External Redirects Test</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from "astro/config";
import cloudflare from "@astrojs/cloudflare";

export default defineConfig({
adapter: cloudflare(),
output: 'static',
redirects: {
"/redirect": '/page2'
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@test/astro-cloudflare-internal-redirects",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "astro build"
},
"dependencies": {
"@astrojs/cloudflare": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
---
<html>
<head>
<title>Internal Redirects Test</title>
</head>
<body>
<h1>Internal Redirects Test</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
---
<html>
<head>
<title>Page 2</title>
</head>
<body>
<h1>Page 2</h1>
</body>
</html>
35 changes: 35 additions & 0 deletions packages/integrations/cloudflare/test/internal-redirects.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, it } from 'node:test';
import { loadFixture } from './_test-utils.js';
import assert from 'node:assert/strict';
import { existsSync, readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';

describe('Internal Redirects', () => {
let fixture;

it('should not create a prerendered file for internal redirects', async () => {
fixture = await loadFixture({
root: './fixtures/internal-redirects',
});

await fixture.build();

// Check that the redirect file was created and contains the redirect
const redirectsPath = fileURLToPath(new URL('client/_redirects', fixture.config.outDir));
const redirectsContent = readFileSync(redirectsPath, 'utf-8');
assert.match(
redirectsContent,
/\/redirect\s+\/page2\s+301/,
'_redirects file should contain the redirect rule',
);

// Check that the destination was not prerendered
const prerenderedPath = fileURLToPath(
new URL('client/redirect/index.html', fixture.config.outDir),
);
assert.ok(
!existsSync(prerenderedPath),
'Should not create prerendered file for internal redirect destination',
);
});
});
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading