Skip to content

Commit

Permalink
Add support for matchers and required language (#32)
Browse files Browse the repository at this point in the history
* feat: allow required language or route matcher

Signed-off-by: Jade Ellis <[email protected]>

* fix: required lang should always fill lang param

Signed-off-by: Jade Ellis <[email protected]>

* fix: don't filter required lang parameters from route samples

* docs: clarify sitemap comment

* fix: remove debug log

Co-authored-by: Jason <[email protected]>

---------

Signed-off-by: Jade Ellis <[email protected]>
Co-authored-by: Jason <[email protected]>
  • Loading branch information
JadedBlueEyes and jasongitmail authored Jul 3, 2024
1 parent 59b1056 commit 21f07f7
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 193 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,11 @@ language versions of your pages.
1. Create a directory named `[[lang]]` at `src/routes/[[lang]]`. Place any
routes that you intend to translate inside here.

**This must be named `[[lang]]`.** It can be within a group if you want, e.g.
**This parameter must be named `lang`.** It can be within a group if you want, e.g.
`src/routes/(public)/[[lang]]`.

To require a language to be specified, name the directory `[lang]`. You may also use a [matcher](https://kit.svelte.dev/docs/advanced-routing#matching).

2. Within your `sitemap.xml` route, update your Super Sitemap config object to
add a `lang` property specifying your desired languages.

Expand Down
169 changes: 0 additions & 169 deletions src/lib/directory-tree.js

This file was deleted.

5 changes: 3 additions & 2 deletions src/lib/sampled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export async function _sampledUrls(sitemapXml: string): Promise<string[]> {
//https://kit.svelte.dev/docs/advanced-routing#advanced-layouts-breaking-out-of-layouts
routes = routes.filter((route) => route.match(/\+page.*\.svelte$/));


// 1. Trim everything to left of '/src/routes/' so it starts with
// `src/routes/` as `filterRoutes()` expects.
// 2. Remove all grouping segments. i.e. those starting with '(' and ending
Expand All @@ -149,11 +150,11 @@ export async function _sampledUrls(sitemapXml: string): Promise<string[]> {
// generation of the sitemap.
routes = filterRoutes(routes, []);

// Remove any `/[[lang]]` prefix. We can just use the default language that
// Remove any optional `/[[lang]]` prefix. We can just use the default language that
// will not have this stem, for the purposes of this sampling. But ensure root
// becomes '/', not an empty string.
routes = routes.map((route) => {
return route.replace('/[[lang]]', '') || '/';
return route.replace(/\/?\[\[lang(=[a-z]+)?\]\]/, '') || '/';
});

// Separate static and dynamic routes. Remember these are _routes_ from disk
Expand Down
95 changes: 94 additions & 1 deletion src/lib/sitemap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,33 @@ describe('sitemap.ts', () => {
const result = sitemap.processRoutesForOptionalParams(routes);
expect(result).toEqual(expected);
});

it('when /[lang] exists, should process routes with optional parameters correctly', () => {
const routes = [
'/[lang=lang]',
'/[lang]/foo/[[paramA]]',
'/[lang]/foo/bar/[paramB]/[[paramC]]/[[paramD]]',
'/[lang]/product/[id]',
'/[lang]/other',
];
const expected = [
'/[lang=lang]',
// route 0
'/[lang]/foo',
'/[lang]/foo/[[paramA]]',
// route 1
'/[lang]/foo/bar/[paramB]',
'/[lang]/foo/bar/[paramB]/[[paramC]]',
'/[lang]/foo/bar/[paramB]/[[paramC]]/[[paramD]]',
// route 2
'/[lang]/product/[id]',
// route 3
'/[lang]/other',
];

const result = sitemap.processRoutesForOptionalParams(routes);
expect(result).toEqual(expected);
});
});

describe('processOptionalParams()', () => {
Expand Down Expand Up @@ -926,7 +953,7 @@ describe('sitemap.ts', () => {
});

describe('generatePathsWithlang()', () => {
const paths = ['/', '/about', '/foo/something'];
const paths = ['/[[lang]]', '/[[lang]]/about', '/[[lang]]/foo/something'];
const langConfig: LangConfig = {
default: 'en',
alternates: ['de', 'es'],
Expand Down Expand Up @@ -990,4 +1017,70 @@ describe('sitemap.ts', () => {
expect(result).toEqual(expected);
});
});

describe('generatePathsWithRequiredlang()', () => {
const paths = ['/[lang]', '/[lang]/about', '/[lang]/foo/something'];
const langConfig: LangConfig = {
default: 'en',
alternates: ['de', 'es'],
};

it('should return expected objects for all paths', () => {
const result = sitemap.generatePathsWithLang(paths, langConfig);
const expectedRootAlternates = [
{ lang: 'en', path: '/en' },
{ lang: 'de', path: '/de' },
{ lang: 'es', path: '/es' },
];
const expectedAboutAlternates = [
{ lang: 'en', path: '/en/about' },
{ lang: 'de', path: '/de/about' },
{ lang: 'es', path: '/es/about' },
];
const expectedFooAlternates = [
{ lang: 'en', path: '/en/foo/something' },
{ lang: 'de', path: '/de/foo/something' },
{ lang: 'es', path: '/es/foo/something' },
];
const expected = [
{
path: '/en',
alternates: expectedRootAlternates,
},
{
path: '/de',
alternates: expectedRootAlternates,
},
{
path: '/es',
alternates: expectedRootAlternates,
},
{
path: '/en/about',
alternates: expectedAboutAlternates,
},
{
path: '/de/about',
alternates: expectedAboutAlternates,
},
{
path: '/es/about',
alternates: expectedAboutAlternates,
},
{
path: '/en/foo/something',
alternates: expectedFooAlternates,
},
{
path: '/de/foo/something',
alternates: expectedFooAlternates,
},
{
path: '/es/foo/something',
alternates: expectedFooAlternates,
},
];
expect(result).toEqual(expected);
});
});
});
Loading

0 comments on commit 21f07f7

Please sign in to comment.