2024-01-25 11:17:31 -05:00
|
|
|
import * as assert from 'node:assert/strict';
|
2024-02-21 09:08:19 -05:00
|
|
|
import { after, before, describe, it } from 'node:test';
|
|
|
|
import * as cheerio from 'cheerio';
|
2023-02-02 19:10:16 -05:00
|
|
|
import nodejs from '../dist/index.js';
|
2024-01-25 11:17:31 -05:00
|
|
|
import { loadFixture, waitServerListen } from './test-utils.js';
|
2023-02-02 19:10:16 -05:00
|
|
|
|
2023-05-17 08:23:20 -05: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-02 19:10:16 -05:00
|
|
|
describe('Prerendering', () => {
|
|
|
|
/** @type {import('./test-utils').Fixture} */
|
|
|
|
let fixture;
|
|
|
|
let server;
|
|
|
|
|
2023-05-17 08:23:20 -05:00
|
|
|
describe('With base', async () => {
|
2023-05-15 07:53:34 -05:00
|
|
|
before(async () => {
|
2023-05-17 08:23:20 -05:00
|
|
|
process.env.PRERENDER = true;
|
|
|
|
|
2023-05-15 07:53:34 -05:00
|
|
|
fixture = await loadFixture({
|
|
|
|
base: '/some-base',
|
|
|
|
root: './fixtures/prerender/',
|
|
|
|
output: 'server',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
2023-06-28 07:06:16 -05:00
|
|
|
const { startServer } = await load();
|
2023-05-15 07:53:34 -05:00
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
2024-01-25 11:17:31 -05:00
|
|
|
await waitServerListen(server.server);
|
2023-05-15 07:53:34 -05:00
|
|
|
});
|
2023-02-02 19:10:16 -05:00
|
|
|
|
2023-05-15 07:53:34 -05:00
|
|
|
after(async () => {
|
|
|
|
await server.stop();
|
2023-05-17 08:23:20 -05:00
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
2023-05-15 07:53:34 -05:00
|
|
|
});
|
2023-02-02 19:10:16 -05:00
|
|
|
|
2023-05-15 07:53:34 -05: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-02 19:10:16 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'One');
|
2023-05-15 07:53:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Two');
|
2023-05-15 07:53:34 -05:00
|
|
|
});
|
2023-02-02 19:10:16 -05:00
|
|
|
|
2023-06-06 10:09:00 -05: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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Two');
|
2023-06-06 10:09:00 -05:00
|
|
|
});
|
|
|
|
|
2023-05-15 07:53:34 -05: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-02 19:10:16 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Two');
|
2023-05-15 07:53:34 -05:00
|
|
|
});
|
|
|
|
|
2024-01-25 05:23:27 -05:00
|
|
|
it('Can render prerendered route without trailing slash', async () => {
|
2023-05-15 07:53:34 -05:00
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/two`, {
|
|
|
|
redirect: 'manual',
|
|
|
|
});
|
2024-01-25 05:23:27 -05:00
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
2024-02-01 16:24:21 -05:00
|
|
|
assert.equal(res.status, 200);
|
2024-01-25 11:18:40 -05:00
|
|
|
assert.equal($('h1').text(), 'Two');
|
2023-05-15 07:53:34 -05:00
|
|
|
});
|
2023-02-02 19:10:16 -05:00
|
|
|
});
|
2023-05-17 08:23:20 -05:00
|
|
|
|
|
|
|
describe('Without base', async () => {
|
2023-05-15 07:53:34 -05:00
|
|
|
before(async () => {
|
2023-05-17 08:23:20 -05:00
|
|
|
process.env.PRERENDER = true;
|
|
|
|
|
2023-05-15 07:53:34 -05: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;
|
2024-01-25 11:17:31 -05:00
|
|
|
await waitServerListen(server.server);
|
2023-05-15 07:53:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await server.stop();
|
2023-05-17 08:23:20 -05:00
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
2023-05-15 07:53:34 -05: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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'One');
|
2023-05-15 07:53:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Two');
|
2023-05-15 07:53:34 -05:00
|
|
|
});
|
|
|
|
|
2023-06-06 10:09:00 -05: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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Two');
|
2023-06-06 10:09:00 -05:00
|
|
|
});
|
|
|
|
|
2023-05-15 07:53:34 -05: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 09:01:05 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Two');
|
2023-05-12 09:01:05 -05:00
|
|
|
});
|
|
|
|
});
|
2024-01-24 09:42:24 -05:00
|
|
|
|
|
|
|
describe('Dev', () => {
|
|
|
|
let devServer;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
process.env.PRERENDER = true;
|
|
|
|
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/prerender/',
|
|
|
|
output: 'server',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
devServer = await fixture.startDevServer();
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await devServer.stop();
|
|
|
|
delete process.env.PRERENDER;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render SSR route', async () => {
|
|
|
|
const res = await fixture.fetch(`/one`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
2024-02-01 16:24:21 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'One');
|
2024-01-24 09:42:24 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render prerendered route', async () => {
|
|
|
|
const res = await fixture.fetch(`/two`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
2024-02-01 16:24:21 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Two');
|
2024-01-24 09:42:24 -05:00
|
|
|
});
|
|
|
|
});
|
2023-02-02 19:10:16 -05:00
|
|
|
});
|
2023-05-17 08:23:20 -05:00
|
|
|
|
|
|
|
describe('Hybrid rendering', () => {
|
|
|
|
/** @type {import('./test-utils').Fixture} */
|
|
|
|
let fixture;
|
|
|
|
let server;
|
|
|
|
|
2024-02-16 09:02:18 -05:00
|
|
|
describe('With base', () => {
|
2023-05-17 08:23:20 -05:00
|
|
|
before(async () => {
|
|
|
|
process.env.PRERENDER = false;
|
|
|
|
fixture = await loadFixture({
|
|
|
|
base: '/some-base',
|
|
|
|
root: './fixtures/prerender/',
|
|
|
|
output: 'hybrid',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
2024-01-26 12:17:32 -05:00
|
|
|
const { startServer } = await load();
|
2023-05-17 08:23:20 -05:00
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
2024-01-25 11:17:31 -05:00
|
|
|
await waitServerListen(server.server);
|
2023-05-17 08:23:20 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Two');
|
2023-05-17 08:23:20 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'One');
|
2023-05-17 08:23:20 -05:00
|
|
|
});
|
|
|
|
|
2023-06-06 10:09:00 -05: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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'One');
|
2023-06-06 10:09:00 -05:00
|
|
|
});
|
|
|
|
|
2023-05-17 08:23:20 -05: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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'One');
|
2023-05-17 08:23:20 -05:00
|
|
|
});
|
|
|
|
|
2024-01-25 05:23:27 -05:00
|
|
|
it('Can render prerendered route without trailing slash', async () => {
|
2023-05-17 08:23:20 -05:00
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/one`, {
|
|
|
|
redirect: 'manual',
|
|
|
|
});
|
2024-02-01 16:24:21 -05:00
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
assert.equal(res.status, 200);
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal($('h1').text(), 'One');
|
2023-05-17 08:23:20 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-02-16 09:02:18 -05:00
|
|
|
describe('Without base', () => {
|
2023-05-17 08:23:20 -05:00
|
|
|
before(async () => {
|
|
|
|
process.env.PRERENDER = false;
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/prerender/',
|
|
|
|
output: 'hybrid',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
2024-01-26 12:17:32 -05:00
|
|
|
const { startServer } = await load();
|
2023-05-17 08:23:20 -05:00
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
2024-01-25 11:17:31 -05:00
|
|
|
await waitServerListen(server.server);
|
2023-05-17 08:23:20 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Two');
|
2023-05-17 08:23:20 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'One');
|
2023-05-17 08:23:20 -05:00
|
|
|
});
|
|
|
|
|
2023-06-06 10:09:00 -05: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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'One');
|
2023-06-06 10:09:00 -05:00
|
|
|
});
|
|
|
|
|
2023-05-17 08:23:20 -05: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);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'One');
|
2023-05-17 08:23:20 -05:00
|
|
|
});
|
|
|
|
});
|
2024-01-26 12:17:32 -05:00
|
|
|
|
2024-02-16 09:02:18 -05:00
|
|
|
describe('Shared modules', () => {
|
2024-01-26 12:17:32 -05:00
|
|
|
before(async () => {
|
|
|
|
process.env.PRERENDER = false;
|
|
|
|
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/prerender/',
|
|
|
|
output: 'hybrid',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
const { startServer } = await load();
|
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
2024-04-05 08:30:59 -05:00
|
|
|
await waitServerListen(server.server);
|
2024-01-26 12:17:32 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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}/third`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
2024-02-01 16:24:21 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'shared');
|
2024-01-26 12:17:32 -05:00
|
|
|
});
|
|
|
|
});
|
2023-05-17 08:23:20 -05:00
|
|
|
});
|