try to make it work

This commit is contained in:
sebastien 2024-02-16 16:47:44 +01:00
parent 3426f848a3
commit d0e61e8d55
4 changed files with 23 additions and 3 deletions

View file

@ -36,16 +36,24 @@ function assertIsHtmlTagObject(val: unknown): asserts val is HtmlTagObject {
} }
} }
function absoluteToRelativeTagAttribute(name: string, value: string): string {
if ((name === 'src' || name === 'href') && value.startsWith('/')) {
return `.${value}`; // TODO would only work for homepage
}
return value;
}
function htmlTagObjectToString(tag: unknown): string { function htmlTagObjectToString(tag: unknown): string {
assertIsHtmlTagObject(tag); assertIsHtmlTagObject(tag);
const isVoidTag = (voidHtmlTags as string[]).includes(tag.tagName); const isVoidTag = (voidHtmlTags as string[]).includes(tag.tagName);
const tagAttributes = tag.attributes ?? {}; const tagAttributes = tag.attributes ?? {};
const attributes = Object.keys(tagAttributes) const attributes = Object.keys(tagAttributes)
.map((attr) => { .map((attr) => {
const value = tagAttributes[attr]!; let value = tagAttributes[attr]!;
if (typeof value === 'boolean') { if (typeof value === 'boolean') {
return value ? attr : undefined; return value ? attr : undefined;
} }
value = absoluteToRelativeTagAttribute(attr, value);
return `${attr}="${escapeHTML(value)}"`; return `${attr}="${escapeHTML(value)}"`;
}) })
.filter((str): str is string => Boolean(str)); .filter((str): str is string => Boolean(str));

View file

@ -182,6 +182,7 @@ async function generateStaticFile({
}); });
// This renders the full page HTML, including head tags... // This renders the full page HTML, including head tags...
const fullPageHtml = renderSSRTemplate({ const fullPageHtml = renderSSRTemplate({
pathname,
params, params,
result, result,
}); });

View file

@ -63,9 +63,11 @@ function getScriptsAndStylesheets({
} }
export function renderSSRTemplate({ export function renderSSRTemplate({
pathname,
params, params,
result, result,
}: { }: {
pathname: string;
params: SSGParams; params: SSGParams;
result: AppRenderResult; result: AppRenderResult;
}): string { }): string {
@ -96,9 +98,18 @@ export function renderSSRTemplate({
]; ];
const metaAttributes = metaStrings.filter(Boolean); const metaAttributes = metaStrings.filter(Boolean);
const numberOfSlashes = pathname.match(/\//g)?.length ?? 0;
const local = true;
const localBaseUrl =
numberOfSlashes === 1 ? `./` : '../'.repeat(numberOfSlashes - 1);
// console.log({pathname, numberOfSlashes, baseUrl, finalBaseUrl, headTags});
const data: SSRTemplateData = { const data: SSRTemplateData = {
appHtml, appHtml,
baseUrl, baseUrl: local ? localBaseUrl : baseUrl,
htmlAttributes, htmlAttributes,
bodyAttributes, bodyAttributes,
headTags, headTags,

View file

@ -112,7 +112,7 @@ export async function createBaseConfig({
chunkFilename: isProd chunkFilename: isProd
? 'assets/js/[name].[contenthash:8].js' ? 'assets/js/[name].[contenthash:8].js'
: '[name].js', : '[name].js',
publicPath: baseUrl, publicPath: isServer ? baseUrl : 'auto',
hashFunction: 'xxhash64', hashFunction: 'xxhash64',
}, },
// Don't throw warning when asset created is over 250kb // Don't throw warning when asset created is over 250kb