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): hash router option - browse site offline (experimental) #9859

Merged
merged 53 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
27b1acf
attempt to use hash-based history and make it work offline
slorber Feb 16, 2024
da45fff
Merge branch 'main' into slorber/offline-mode-poc-2
slorber Feb 22, 2024
de0df2d
Add router type to config file
slorber Feb 22, 2024
0c5034b
fix tests
slorber Feb 22, 2024
45c1c9d
add link hack
slorber Feb 22, 2024
25a9004
disable redirect plugin with the hash router?
slorber Feb 22, 2024
904f730
add siteConfig DOCUSAURUS_ROUTER env variable
slorber Feb 22, 2024
a1b9ba6
logging message
slorber Feb 22, 2024
547d8ab
sitemap should support hash router?
slorber Feb 22, 2024
4c5a9de
Disable PWA/client-redirects plugins in Hash Router mode
slorber Feb 22, 2024
c22da5a
Fix heading/TOC/anchor links with hash router
slorber Feb 22, 2024
4a22ebb
urlUtils normalizeUrl should support hash router
slorber Feb 23, 2024
5a5d2f3
simplify feeds unit tests
slorber Feb 23, 2024
81dbd82
blog feed support for Hash Router
slorber Feb 23, 2024
6c072c3
remove comment
slorber Feb 23, 2024
bffb2f0
add unit test for sitemap with hash router
slorber Feb 23, 2024
030688a
Add router config type doc
slorber Feb 23, 2024
62aa2cb
Make it possible to switch router dynamically
slorber Feb 23, 2024
6582ea7
Rework full build workflow for hash router support
slorber Feb 23, 2024
2f33aad
refactor: apply lint autofix
slorber Feb 23, 2024
e902401
Merge branch 'main' into slorber/offline-mode-poc-2
slorber May 17, 2024
b82e8a5
add missing route codegen after merge
slorber May 17, 2024
abc3a31
Encapsulate router impl switch in docusaurus core to avoid pnpm / yar…
slorber May 17, 2024
62fc20e
rename siteConfig import
slorber May 17, 2024
b80d22c
disable sitemap plugin with hash router
slorber May 17, 2024
e8ef286
disable blog feeds for hash router
slorber May 17, 2024
6282cca
improve disabled plugin warning
slorber May 17, 2024
b7d2884
Fix router type
slorber May 17, 2024
2c323f1
Move router config to siteConfig.future.experimental_router + add con…
slorber May 17, 2024
520bc92
fix webpack client config
slorber May 17, 2024
61f453e
fix unit tests breaking due to missing siteConfig.future
slorber May 17, 2024
62a25c5
Fix Docusaurus website config
slorber May 17, 2024
0a24bb7
simpler router switch
slorber May 17, 2024
9bbb135
simpler router switch
slorber May 17, 2024
15cc555
fix dev server with hash router + baseUrl
slorber May 17, 2024
f12aeee
fix hash router build
slorber May 17, 2024
bbc94c7
do not emit Algolia opensearch file with hash router
slorber May 17, 2024
533e307
Extract StaticDirectoriesCopyPlugin + move it to the client webpack c…
slorber May 17, 2024
1dad032
Add some tests for useBaseUrl with hash router
slorber May 17, 2024
f8edab1
basic router type docs
slorber May 17, 2024
b1f2c8d
revert useless serverEntry hack
slorber May 17, 2024
0465e7e
simplify ssg params
slorber May 17, 2024
a36ab2e
Add CI workflow for build with hash router
slorber May 17, 2024
905e681
fix CI workflow?
slorber May 17, 2024
4b7a8c2
only apply hash router logic when the hash router is enabled for html…
slorber May 17, 2024
db860a8
Add hack comment
slorber May 17, 2024
d877806
deploy hash router to github pages
slorber May 19, 2024
2b5e458
deploy hash router to github pages
slorber May 19, 2024
19745dc
deploy hash router to github pages
slorber May 19, 2024
a732f95
deploy hash router to github pages
slorber May 19, 2024
3610306
deploy hash router to github pages
slorber May 19, 2024
2de9c18
deploy hash router to github pages
slorber May 19, 2024
7cf6500
deploy hash router to github pages
slorber May 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 60 additions & 0 deletions .github/workflows/build-hash-router.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Build Hash Router

on:
pull_request:
branches:
- main
- docusaurus-v**
paths:
- packages/**

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
build:
name: Build Hash Router
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Set up Node
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: '18'
cache: yarn
- name: Installation
run: yarn
- name: Build Hash Router
run: yarn build:website:fast
env:
DOCUSAURUS_ROUTER: 'hash'
BASE_URL: '/docusaurus/' # GH pages deploys under https://facebook.github.io/docusaurus/
- name: Upload Website artifact
uses: actions/upload-artifact@v4
with:
name: website-hash-router-archive
path: website/build
- name: Upload Website Pages artifact
uses: actions/upload-pages-artifact@v3
with:
path: website/build

# See https://docusaurus.io/docs/deployment#triggering-deployment-with-github-actions
deploy:
name: Deploy to GitHub Pages
if: ${{ github.event_name != 'pull_request' && github.ref_name == 'main')}}
needs: build
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
13 changes: 12 additions & 1 deletion packages/docusaurus-plugin-client-redirects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import {addLeadingSlash, removePrefix} from '@docusaurus/utils-common';
import logger from '@docusaurus/logger';
import collectRedirects from './collectRedirects';
import writeRedirectFiles, {
toRedirectFiles,
Expand All @@ -15,14 +16,24 @@ import type {LoadContext, Plugin} from '@docusaurus/types';
import type {PluginContext, RedirectItem} from './types';
import type {PluginOptions, Options} from './options';

const PluginName = 'docusaurus-plugin-client-redirects';

export default function pluginClientRedirectsPages(
context: LoadContext,
options: PluginOptions,
): Plugin<void> {
const {trailingSlash} = context.siteConfig;
const router = context.siteConfig.future.experimental_router;

if (router === 'hash') {
logger.warn(
`${PluginName} does not support the Hash Router and will be disabled.`,
);
return {name: PluginName};
}

return {
name: 'docusaurus-plugin-client-redirects',
name: PluginName,
async postBuild(props) {
const pluginContext: PluginContext = {
relativeRoutesPaths: props.routesPaths.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const getPlugin = async (
baseUrl: '/',
url: 'https://docusaurus.io',
markdown,
future: {},
} as DocusaurusConfig;
return pluginContentBlog(
{
Expand Down
58 changes: 57 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
applyTrailingSlash,
} from '@docusaurus/utils-common';
import {load as cheerioLoad} from 'cheerio';
import type {DocusaurusConfig} from '@docusaurus/types';
import type {DocusaurusConfig, HtmlTags, LoadContext} from '@docusaurus/types';
import type {
FeedType,
PluginOptions,
Expand Down Expand Up @@ -254,3 +254,59 @@ export async function createBlogFeedFiles({
),
);
}

export function createFeedHtmlHeadTags({
context,
options,
}: {
context: LoadContext;
options: PluginOptions;
}): HtmlTags {
const feedTypes = options.feedOptions.type;
if (!feedTypes) {
return [];
}
const feedTitle = options.feedOptions.title ?? context.siteConfig.title;
const feedsConfig = {
rss: {
type: 'application/rss+xml',
path: 'rss.xml',
title: `${feedTitle} RSS Feed`,
},
atom: {
type: 'application/atom+xml',
path: 'atom.xml',
title: `${feedTitle} Atom Feed`,
},
json: {
type: 'application/json',
path: 'feed.json',
title: `${feedTitle} JSON Feed`,
},
};
const headTags: HtmlTags = [];

feedTypes.forEach((feedType) => {
const {
type,
path: feedConfigPath,
title: feedConfigTitle,
} = feedsConfig[feedType];

headTags.push({
tagName: 'link',
attributes: {
rel: 'alternate',
type,
href: normalizeUrl([
context.siteConfig.baseUrl,
options.routeBasePath,
feedConfigPath,
]),
title: feedConfigTitle,
},
});
});

return headTags;
}
91 changes: 30 additions & 61 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import {
} from './blogUtils';
import footnoteIDFixer from './remark/footnoteIDFixer';
import {translateContent, getTranslationFiles} from './translations';
import {createBlogFeedFiles} from './feed';
import {createBlogFeedFiles, createFeedHtmlHeadTags} from './feed';

import {createAllRoutes} from './routes';
import type {BlogContentPaths, BlogMarkdownLoaderOptions} from './types';
import type {LoadContext, Plugin, HtmlTags} from '@docusaurus/types';
import type {LoadContext, Plugin} from '@docusaurus/types';
import type {
PluginOptions,
BlogPostFrontMatter,
Expand All @@ -44,6 +44,8 @@ import type {
BlogPaginated,
} from '@docusaurus/plugin-content-blog';

const PluginName = 'docusaurus-plugin-content-blog';

export default async function pluginContentBlog(
context: LoadContext,
options: PluginOptions,
Expand All @@ -55,22 +57,29 @@ export default async function pluginContentBlog(
localizationDir,
i18n: {currentLocale},
} = context;

const router = siteConfig.future.experimental_router;
const isBlogFeedDisabledBecauseOfHashRouter =
router === 'hash' && !!options.feedOptions.type;
if (isBlogFeedDisabledBecauseOfHashRouter) {
logger.warn(
`${PluginName} feed feature does not support the Hash Router. Feeds won't be generated.`,
);
}

const {onBrokenMarkdownLinks, baseUrl} = siteConfig;

const contentPaths: BlogContentPaths = {
contentPath: path.resolve(siteDir, options.path),
contentPathLocalized: getPluginI18nPath({
localizationDir,
pluginName: 'docusaurus-plugin-content-blog',
pluginName: PluginName,
pluginId: options.id,
}),
};
const pluginId = options.id ?? DEFAULT_PLUGIN_ID;

const pluginDataDirRoot = path.join(
generatedFilesDir,
'docusaurus-plugin-content-blog',
);
const pluginDataDirRoot = path.join(generatedFilesDir, PluginName);
const dataDir = path.join(pluginDataDirRoot, pluginId);
// TODO Docusaurus v4 breaking change
// module aliasing should be automatic
Expand All @@ -84,7 +93,7 @@ export default async function pluginContentBlog(
});

return {
name: 'docusaurus-plugin-content-blog',
name: PluginName,

getPathsToWatch() {
const {include} = options;
Expand Down Expand Up @@ -295,15 +304,16 @@ export default async function pluginContentBlog(
},

async postBuild({outDir, content}) {
if (!options.feedOptions.type) {
return;
}
const {blogPosts} = content;
if (!blogPosts.length) {
if (
!content.blogPosts.length ||
!options.feedOptions.type ||
isBlogFeedDisabledBecauseOfHashRouter
) {
return;
}

await createBlogFeedFiles({
blogPosts,
blogPosts: content.blogPosts,
options,
outDir,
siteConfig,
Expand All @@ -312,56 +322,15 @@ export default async function pluginContentBlog(
},

injectHtmlTags({content}) {
if (!content.blogPosts.length || !options.feedOptions.type) {
if (
!content.blogPosts.length ||
!options.feedOptions.type ||
isBlogFeedDisabledBecauseOfHashRouter
) {
return {};
}

const feedTypes = options.feedOptions.type;
const feedTitle = options.feedOptions.title ?? context.siteConfig.title;
const feedsConfig = {
rss: {
type: 'application/rss+xml',
path: 'rss.xml',
title: `${feedTitle} RSS Feed`,
},
atom: {
type: 'application/atom+xml',
path: 'atom.xml',
title: `${feedTitle} Atom Feed`,
},
json: {
type: 'application/json',
path: 'feed.json',
title: `${feedTitle} JSON Feed`,
},
};
const headTags: HtmlTags = [];

feedTypes.forEach((feedType) => {
const {
type,
path: feedConfigPath,
title: feedConfigTitle,
} = feedsConfig[feedType];

headTags.push({
tagName: 'link',
attributes: {
rel: 'alternate',
type,
href: normalizeUrl([
baseUrl,
options.routeBasePath,
feedConfigPath,
]),
title: feedConfigTitle,
},
});
});

return {
headTags,
};
return {headTags: createFeedHtmlHeadTags({context, options})};
},
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@babel/core": "^7.23.3",
"@babel/preset-env": "^7.23.3",
"@docusaurus/core": "3.3.2",
"@docusaurus/logger": "3.3.2",
"@docusaurus/theme-common": "3.3.2",
"@docusaurus/theme-translations": "3.3.2",
"@docusaurus/types": "3.3.2",
Expand Down
13 changes: 12 additions & 1 deletion packages/docusaurus-plugin-pwa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import WebpackBar from 'webpackbar';
import Terser from 'terser-webpack-plugin';
import {injectManifest} from 'workbox-build';
import {normalizeUrl} from '@docusaurus/utils';
import logger from '@docusaurus/logger';
import {compile} from '@docusaurus/core/lib/webpack/utils';
import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations';
import type {HtmlTags, LoadContext, Plugin} from '@docusaurus/types';
import type {PluginOptions} from '@docusaurus/plugin-pwa';

const PluginName = 'docusaurus-plugin-pwa';

const isProd = process.env.NODE_ENV === 'production';

function getSWBabelLoader() {
Expand Down Expand Up @@ -47,6 +50,7 @@ export default function pluginPWA(
outDir,
baseUrl,
i18n: {currentLocale},
siteConfig,
} = context;
const {
debug,
Expand All @@ -57,8 +61,15 @@ export default function pluginPWA(
swRegister,
} = options;

if (siteConfig.future.experimental_router === 'hash') {
logger.warn(
`${PluginName} does not support the Hash Router and will be disabled.`,
);
return {name: PluginName};
}

return {
name: 'docusaurus-plugin-pwa',
name: PluginName,

getThemePath() {
return '../lib/theme';
Expand Down
11 changes: 10 additions & 1 deletion packages/docusaurus-plugin-sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ import createSitemap from './createSitemap';
import type {PluginOptions, Options} from './options';
import type {LoadContext, Plugin} from '@docusaurus/types';

const PluginName = 'docusaurus-plugin-sitemap';

export default function pluginSitemap(
context: LoadContext,
options: PluginOptions,
): Plugin<void> {
if (context.siteConfig.future.experimental_router === 'hash') {
logger.warn(
`${PluginName} does not support the Hash Router and will be disabled.`,
);
return {name: PluginName};
}

return {
name: 'docusaurus-plugin-sitemap',
name: PluginName,

async postBuild({siteConfig, routes, outDir, head}) {
if (siteConfig.noIndex) {
Expand Down