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

fix: static export #1

Merged
merged 1 commit into from
Sep 28, 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
79 changes: 73 additions & 6 deletions packages/react-server/lib/build/static.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ export default async function staticSiteGenerator(root, options) {
const render = await ssrHandler();
await Promise.all(
paths.map(async ({ path }) => {
const url = new URL(
`http${config.server?.https ? "s" : ""}://${
config.host ?? "localhost"
}:${config.port ?? 3000}${path}`
);
const stream = await render({
url,
request: {
url: `http${config.server?.https ? "s" : ""}://${
config.host ?? "localhost"
}:${config.port ?? 3000}${path}`,
url,
headers: new Headers({
accept: "text/html",
}),
Expand All @@ -53,7 +57,67 @@ export default async function staticSiteGenerator(root, options) {
await mkdir(join(cwd(), ".react-server/dist", path), {
recursive: true,
});
const basename = `${path.replace(/^\/+/g, "")}/index.html`;
const normalizedPath = path.replace(/^\/+/g, "");
const basename = `${normalizedPath}/index.html`.replace(/^\/+/g, "");
const filename = join(cwd(), ".react-server/dist", basename);
const gzip = createGzip();
const brotli = createBrotliCompress();
const gzipWriteStream = createWriteStream(`${filename}.gz`);
const brotliWriteStream = createWriteStream(`${filename}.br`);
await Promise.all([
pipeline(Readable.from(html), gzip, gzipWriteStream),
pipeline(Readable.from(html), brotli, brotliWriteStream),
writeFile(filename, html, "utf8"),
]);
const [htmlStat, gzipStat, brotliStat] = await Promise.all([
stat(filename),
stat(`${filename}.gz`),
stat(`${filename}.br`),
]);

console.log(
`${colors.dim(".react-server/dist/")}${colors.green(
path.length < 30
? `${normalizedPath}${colors.cyan(
`${path.replace(/^\/+/g, "") ? "/" : ""}index.html`
)}`
: `${normalizedPath.slice(0, 27)}...`
)}${`${" ".repeat(Math.max(0, 30 - path.length))}${colors.gray(
colors.bold(size(htmlStat.size))
)} ${colors.dim(
`│ gzip: ${size(gzipStat.size)} │ brotli: ${size(
brotliStat.size
)}`
)}`}`
);
})
);

await Promise.all(
paths.map(async ({ path }) => {
const url = new URL(
`http${config.server?.https ? "s" : ""}://${
config.host ?? "localhost"
}:${config.port ?? 3000}${path}`
);
const stream = await render({
url,
request: {
url,
headers: new Headers({
accept: "text/x-component",
}),
},
});
const html = await stream.text();
await mkdir(join(cwd(), ".react-server/dist", path), {
recursive: true,
});
const normalizedPath = path.replace(/^\/+/g, "");
const basename = `${normalizedPath}/x-component.rsc`.replace(
/^\/+/g,
""
);
const filename = join(cwd(), ".react-server/dist", basename);
const gzip = createGzip();
const brotli = createBrotliCompress();
Expand All @@ -69,11 +133,14 @@ export default async function staticSiteGenerator(root, options) {
stat(`${filename}.gz`),
stat(`${filename}.br`),
]);

console.log(
`${colors.dim(".react-server/dist/")}${colors.green(
path.length < 30
? `${path.replace(/^\/+/g, "")}${colors.cyan("/index.html")}`
: `${path.replace(/^\/+/g, "").slice(0, 27)}...`
? `${normalizedPath}${colors.cyan(
`${path.replace(/^\/+/g, "") ? "/" : ""}x-component.rsc`
)}`
: `${normalizedPath.slice(0, 27)}...`
)}${`${" ".repeat(Math.max(0, 30 - path.length))}${colors.gray(
colors.bold(size(htmlStat.size))
)} ${colors.dim(
Expand Down
30 changes: 24 additions & 6 deletions packages/react-server/lib/handlers/static.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,32 @@ export default async function staticHandler(dir, options = {}) {
const isBrotli = acceptEncoding?.includes("br");
const isGzip = acceptEncoding?.includes("gzip");

if (isBrotli && files.has(`${pathname}/index.html.br`)) {
pathname = `${pathname}/index.html.br`;
const basename = `${pathname}/index.html`.replace(/^\/+/g, "");
if (isBrotli && files.has(`${basename}.br`)) {
pathname = `${basename}.br`;
contentEncoding = "br";
} else if (isGzip && files.has(`${pathname}/index.html.gz`)) {
pathname = `${pathname}/index.html.gz`;
} else if (isGzip && files.has(`${basename}.gz`)) {
pathname = `${basename}.gz`;
contentEncoding = "gzip";
} else if (files.has(`${pathname}/index.html`)) {
pathname = `${pathname}/index.html`;
} else if (files.has(basename)) {
pathname = basename;
}
}

if (isRSC) {
const acceptEncoding = context.request.headers.get("accept-encoding");
const isBrotli = acceptEncoding?.includes("br");
const isGzip = acceptEncoding?.includes("gzip");

const basename = `${pathname}/x-component.rsc`.replace(/^\/+/g, "");
if (isBrotli && files.has(`${basename}.br`)) {
pathname = `${basename}.br`;
contentEncoding = "br";
} else if (isGzip && files.has(`${basename}.gz`)) {
pathname = `${basename}.gz`;
contentEncoding = "gzip";
} else if (files.has(basename)) {
pathname = basename;
}
}

Expand Down