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

feat(core): support an offline mode build - SSG solution #9857

Closed
wants to merge 1 commit into from
Closed
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
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
Loading