Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support redirects in Netlify SSR configuration #7167

Merged
merged 1 commit into from
May 23, 2023
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
10 changes: 0 additions & 10 deletions packages/integrations/netlify/src/integration-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ export function netlifyStatic(): AstroIntegration {
'astro:config:done': ({ config }) => {
_config = config;
},
// 'astro:config:setup': ({ config, updateConfig }) => {
// const outDir = dist ?? new URL('./dist/', config.root);
// updateConfig({
// outDir,
// build: {
// client: outDir,
// server: new URL('./.netlify/functions-internal/', config.root),
// },
// });
// },
'astro:build:done': async ({ dir, routes }) => {
await createRedirects(_config, routes, dir, '', 'static');
}
Expand Down
29 changes: 17 additions & 12 deletions packages/integrations/netlify/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ export async function createRedirects(

for (const route of routes) {
if (route.pathname) {
if( kind === 'static') {
if(route.redirect) {
definitions.push({
dynamic: false,
input: route.pathname,
target: route.redirect,
status: 301,
weight: 1
});
}
if(route.redirect) {
definitions.push({
dynamic: false,
input: route.pathname,
target: route.redirect,
status: 301,
weight: 1
});
continue;
}

if(kind === 'static') {
continue;
}
else if (route.distURL) {
Expand Down Expand Up @@ -80,7 +82,7 @@ export async function createRedirects(
})
.join('/');

if(kind === 'static') {
//if(kind === 'static') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To remove?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, removed in this follow up: #7173

if(route.redirect) {
definitions.push({
dynamic: true,
Expand All @@ -89,8 +91,11 @@ export async function createRedirects(
status: 301,
weight: 1
});
continue;
}
continue;

if(kind === 'static') {
continue;
}
else if (route.distURL) {
const target =
Expand Down
39 changes: 39 additions & 0 deletions packages/integrations/netlify/test/functions/redirects.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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': '/'
}
});
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',
'/', '/.netlify/functions/entry', '200',

// 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'
]);
});
});