2023-02-03 00:10:16 +00:00
|
|
|
import nodejs from '../dist/index.js';
|
2023-05-17 13:23:20 +00:00
|
|
|
import { loadFixture } from './test-utils.js';
|
2023-02-03 00:10:16 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as cheerio from 'cheerio';
|
|
|
|
import { fetch } from 'undici';
|
|
|
|
|
2023-05-17 13:23:20 +00:00
|
|
|
/**
|
|
|
|
* @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function load() {
|
|
|
|
const mod = await import(`./fixtures/prerender/dist/server/entry.mjs?dropcache=${Date.now()}`);
|
|
|
|
return mod;
|
|
|
|
}
|
2023-02-03 00:10:16 +00:00
|
|
|
describe('Prerendering', () => {
|
|
|
|
/** @type {import('./test-utils').Fixture} */
|
|
|
|
let fixture;
|
|
|
|
let server;
|
|
|
|
|
2023-05-17 13:23:20 +00:00
|
|
|
describe('With base', async () => {
|
2023-05-15 12:53:34 +00:00
|
|
|
before(async () => {
|
|
|
|
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
|
2023-05-17 13:23:20 +00:00
|
|
|
process.env.PRERENDER = true;
|
|
|
|
|
2023-05-15 12:53:34 +00:00
|
|
|
fixture = await loadFixture({
|
|
|
|
base: '/some-base',
|
|
|
|
root: './fixtures/prerender/',
|
|
|
|
output: 'server',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
const { startServer } = await await load();
|
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
|
|
|
});
|
2023-02-03 00:10:16 +00:00
|
|
|
|
2023-05-15 12:53:34 +00:00
|
|
|
after(async () => {
|
|
|
|
await server.stop();
|
2023-05-17 13:23:20 +00:00
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
2023-05-15 12:53:34 +00:00
|
|
|
});
|
2023-02-03 00:10:16 +00:00
|
|
|
|
2023-05-15 12:53:34 +00:00
|
|
|
it('Can render SSR route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/one`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
2023-02-03 00:10:16 +00:00
|
|
|
|
2023-05-15 12:53:34 +00:00
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('One');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render prerendered route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/two`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('Two');
|
|
|
|
});
|
2023-02-03 00:10:16 +00:00
|
|
|
|
2023-06-06 15:09:00 +00:00
|
|
|
it('Can render prerendered route with redirect and query params', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/two?foo=bar`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('Two');
|
|
|
|
});
|
|
|
|
|
2023-05-15 12:53:34 +00:00
|
|
|
it('Can render prerendered route with query params', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/two/?foo=bar`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
2023-02-03 00:10:16 +00:00
|
|
|
|
2023-05-15 12:53:34 +00:00
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('Two');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Omitting the trailing slash results in a redirect that includes the base', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/two`, {
|
|
|
|
redirect: 'manual',
|
|
|
|
});
|
|
|
|
expect(res.status).to.equal(301);
|
|
|
|
expect(res.headers.get('location')).to.equal('/some-base/two/');
|
|
|
|
});
|
2023-02-03 00:10:16 +00:00
|
|
|
});
|
2023-05-17 13:23:20 +00:00
|
|
|
|
|
|
|
describe('Without base', async () => {
|
2023-05-15 12:53:34 +00:00
|
|
|
before(async () => {
|
|
|
|
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
|
2023-05-17 13:23:20 +00:00
|
|
|
process.env.PRERENDER = true;
|
|
|
|
|
2023-05-15 12:53:34 +00:00
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/prerender/',
|
|
|
|
output: 'server',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
const { startServer } = await await load();
|
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await server.stop();
|
2023-05-17 13:23:20 +00:00
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
2023-05-15 12:53:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render SSR route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/one`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('One');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render prerendered route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/two`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('Two');
|
|
|
|
});
|
|
|
|
|
2023-06-06 15:09:00 +00:00
|
|
|
it('Can render prerendered route with redirect and query params', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/two?foo=bar`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('Two');
|
|
|
|
});
|
|
|
|
|
2023-05-15 12:53:34 +00:00
|
|
|
it('Can render prerendered route with query params', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/two/?foo=bar`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
2023-05-12 14:01:05 +00:00
|
|
|
|
2023-05-15 12:53:34 +00:00
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('Two');
|
2023-05-12 14:01:05 +00:00
|
|
|
});
|
|
|
|
});
|
2023-02-03 00:10:16 +00:00
|
|
|
});
|
2023-05-17 13:23:20 +00:00
|
|
|
|
|
|
|
describe('Hybrid rendering', () => {
|
|
|
|
/** @type {import('./test-utils').Fixture} */
|
|
|
|
let fixture;
|
|
|
|
let server;
|
|
|
|
|
|
|
|
describe('With base', async () => {
|
|
|
|
before(async () => {
|
|
|
|
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
|
|
|
|
process.env.PRERENDER = false;
|
|
|
|
fixture = await loadFixture({
|
|
|
|
base: '/some-base',
|
|
|
|
root: './fixtures/prerender/',
|
|
|
|
output: 'hybrid',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
const { startServer } = await await load();
|
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await server.stop();
|
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render SSR route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/two`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('Two');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render prerendered route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/one`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('One');
|
|
|
|
});
|
|
|
|
|
2023-06-06 15:09:00 +00:00
|
|
|
it('Can render prerendered route with redirect and query params', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/one?foo=bar`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('One');
|
|
|
|
});
|
|
|
|
|
2023-05-17 13:23:20 +00:00
|
|
|
it('Can render prerendered route with query params', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/one/?foo=bar`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('One');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Omitting the trailing slash results in a redirect that includes the base', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/one`, {
|
|
|
|
redirect: 'manual',
|
|
|
|
});
|
|
|
|
expect(res.status).to.equal(301);
|
|
|
|
expect(res.headers.get('location')).to.equal('/some-base/one/');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Without base', async () => {
|
|
|
|
before(async () => {
|
|
|
|
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
|
|
|
|
process.env.PRERENDER = false;
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/prerender/',
|
|
|
|
output: 'hybrid',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
const { startServer } = await await load();
|
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await server.stop();
|
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render SSR route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/two`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('Two');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render prerendered route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/one`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('One');
|
|
|
|
});
|
|
|
|
|
2023-06-06 15:09:00 +00:00
|
|
|
it('Can render prerendered route with redirect and query params', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/one?foo=bar`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('One');
|
|
|
|
});
|
|
|
|
|
2023-05-17 13:23:20 +00:00
|
|
|
it('Can render prerendered route with query params', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/one/?foo=bar`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
expect($('h1').text()).to.equal('One');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|