Skip to content

Commit

Permalink
Build performance optimizations (#2171)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Aug 7, 2024
1 parent 634efdd commit c8258d7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-brooms-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/starlight": patch
---

Improves build performance slightly for bigger sites
3 changes: 2 additions & 1 deletion packages/starlight/user-components/FileTree.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
import { stripLeadingAndTrailingSlashes } from '../utils/path';
import { slugToLocaleData } from '../utils/slugs';
import { useTranslations } from '../utils/translations';
import { processFileTree } from './rehype-file-tree';
const slug = Astro.url.pathname.replace(/^\//, '').replace(/\/$/, '');
const slug = stripLeadingAndTrailingSlashes(Astro.url.pathname);
const t = useTranslations(slugToLocaleData(slug).locale);
const fileTreeHtml = await Astro.slots.render('default');
Expand Down
3 changes: 2 additions & 1 deletion packages/starlight/utils/localizedUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import config from 'virtual:starlight/user-config';
import { stripTrailingSlash } from './path';

/**
* Get the equivalent of the passed URL for the passed locale.
Expand All @@ -12,7 +13,7 @@ export function localizedUrl(url: URL, locale: string | undefined): URL {
}
if (locale === 'root') locale = '';
/** Base URL with trailing `/` stripped. */
const base = import.meta.env.BASE_URL.replace(/\/$/, '');
const base = stripTrailingSlash(import.meta.env.BASE_URL);
const hasBase = url.pathname.startsWith(base);
// Temporarily remove base to simplify
if (hasBase) url.pathname = url.pathname.replace(base, '');
Expand Down
11 changes: 7 additions & 4 deletions packages/starlight/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ function linkFromInternalSidebarLinkItem(
locale: string | undefined,
currentPathname: string
) {
let slugWithLocale = locale ? locale + '/' + item.slug : item.slug;
// Astro passes root `index.[md|mdx]` entries with a slug of `index`
slugWithLocale = slugWithLocale.replace(/\/?index$/, '');
const entry = routes.find((entry) => slugWithLocale === entry.slug);
const slug = item.slug === 'index' ? '' : item.slug;
const localizedSlug = locale ? (slug ? locale + '/' + slug : locale) : slug;
const entry = routes.find((entry) => localizedSlug === entry.slug);
if (!entry) {
const hasExternalSlashes = item.slug.at(0) === '/' || item.slug.at(-1) === '/';
if (hasExternalSlashes) {
Expand Down Expand Up @@ -410,4 +410,7 @@ function applyPrevNextLinkConfig(
}

/** Remove the extension from a path. */
const stripExtension = (path: string) => path.replace(/\.\w+$/, '');
function stripExtension(path: string) {
const periodIndex = path.lastIndexOf('.');
return path.slice(0, periodIndex > -1 ? periodIndex : undefined);
}
7 changes: 4 additions & 3 deletions packages/starlight/utils/slugs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import config from 'virtual:starlight/user-config';
import { BuiltInDefaultLocale } from './i18n';
import { stripTrailingSlash } from './path';

export interface LocaleData {
/** Writing direction. */
Expand Down Expand Up @@ -52,7 +53,7 @@ export function slugToParam(slug: string): string | undefined {
return slug === 'index' || slug === ''
? undefined
: slug.endsWith('/index')
? slug.replace(/\/index$/, '')
? slug.slice(0, -6)
: slug;
}

Expand All @@ -77,7 +78,7 @@ export function localizedSlug(slug: string, locale: string | undefined): string
locale = locale || '';
if (slugLocale === slug) return locale;
if (slugLocale) {
return slug.replace(slugLocale + '/', locale ? locale + '/' : '').replace(/\/$/, '');
return stripTrailingSlash(slug.replace(slugLocale + '/', locale ? locale + '/' : ''));
}
return slug ? locale + '/' + slug : locale;
}
Expand Down Expand Up @@ -106,7 +107,7 @@ export function localizedId(id: string, locale: string | undefined): string {
/** Extract the slug from a URL. */
export function urlToSlug(url: URL): string {
let pathname = url.pathname;
const base = import.meta.env.BASE_URL.replace(/\/$/, '');
const base = stripTrailingSlash(import.meta.env.BASE_URL);
if (pathname.startsWith(base)) pathname = pathname.replace(base, '');
const segments = pathname.split('/');
const htmlExt = '.html';
Expand Down

0 comments on commit c8258d7

Please sign in to comment.