Skip to content

Commit

Permalink
try to make it work
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Feb 16, 2024
1 parent 3426f84 commit d0e61e8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
10 changes: 9 additions & 1 deletion packages/docusaurus/src/server/htmlTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
assertIsHtmlTagObject(tag);
const isVoidTag = (voidHtmlTags as string[]).includes(tag.tagName);
const tagAttributes = tag.attributes ?? {};
const attributes = Object.keys(tagAttributes)
.map((attr) => {
const value = tagAttributes[attr]!;
let value = tagAttributes[attr]!;
if (typeof value === 'boolean') {
return value ? attr : undefined;
}
value = absoluteToRelativeTagAttribute(attr, value);
return `${attr}="${escapeHTML(value)}"`;
})
.filter((str): str is string => Boolean(str));
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus/src/ssg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ async function generateStaticFile({
});
// This renders the full page HTML, including head tags...
const fullPageHtml = renderSSRTemplate({
pathname,
params,
result,
});
Expand Down
13 changes: 12 additions & 1 deletion packages/docusaurus/src/templates/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ function getScriptsAndStylesheets({
}

export function renderSSRTemplate({
pathname,
params,
result,
}: {
pathname: string;
params: SSGParams;
result: AppRenderResult;
}): string {
Expand Down Expand Up @@ -96,9 +98,18 @@ export function renderSSRTemplate({
];
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 = {
appHtml,
baseUrl,
baseUrl: local ? localBaseUrl : baseUrl,
htmlAttributes,
bodyAttributes,
headTags,
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/src/webpack/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function createBaseConfig({
chunkFilename: isProd
? 'assets/js/[name].[contenthash:8].js'
: '[name].js',
publicPath: baseUrl,
publicPath: isServer ? baseUrl : 'auto',
hashFunction: 'xxhash64',
},
// Don't throw warning when asset created is over 250kb
Expand Down

0 comments on commit d0e61e8

Please sign in to comment.