Skip to content

Commit

Permalink
refactor(url/slug): delete unnecessary args
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Jan 26, 2024
1 parent bde0ea1 commit 433b68d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 34 deletions.
22 changes: 4 additions & 18 deletions __tests__/unit/utils/slug.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import { expect, test } from "vitest";
import { sluggize } from "../../../src/utils/url";

test('should sluggize from string array - without prefix and fallback option', () => {
expect(sluggize(['foo', 'bar'])).toEqual('/foo/bar');
test('should sluggize from array', () => {
expect(sluggize(['foo', 'bar'])).toEqual('foo/bar');
});

test('should sluggize from number array - without prefix and fallback option', () => {
expect(sluggize([1, 2])).toEqual('/1/2');
test('should sluggize from array - contains double slashes', () => {
expect(sluggize(['foo//bar', '/hoge/', '/piyo'])).toEqual('foo/bar/hoge/piyo');
});

test('should sluggize from array - without fallback option', () => {
expect(sluggize(['foo', 'bar'], '_prefix')).toEqual('/_prefix/foo/bar');
});

/* TODO: fix
test('should sluggize returns fallback (undefined) if slug can not convert to array', () => {
expect(sluggize({'hoge': 'piyo'}, '_prefix')).toEqual(undefined);
});
test('should sluggize returns fallback string if slug can not convert to array', () => {
expect(sluggize({'hoge': 'piyo'}, '_prefix', 'fallback_str')).toEqual('fallback_str');
});
*/
4 changes: 2 additions & 2 deletions src/app/articles/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const cachedFindByPath = cache(async (path: string) => {

// https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-caching
export async function generateMetadata({ params: { slug }}: { params: { slug: Array<string> }}): Promise<Metadata> {
const sluggized = await sluggize(slug, PREFIX_URL);
const sluggized = await sluggize([PREFIX_URL].concat(slug));
const content = await cachedFindByPath(sluggized);
return generateForArticleOrPage(sluggized ,content.body);
}
Expand All @@ -54,7 +54,7 @@ async function handler(req: any) {
if (isIgnoreRequest(req.params.slug.join("/"))) {
return notFound();
}
const sluggized = await sluggize(req.params.slug, PREFIX_URL);
const sluggized = await sluggize([PREFIX_URL].concat(req.params.slug));
const response: ContentResponseWithFetchResponse = await cachedFindByPath(sluggized);

const content: Content = {
Expand Down
19 changes: 5 additions & 14 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,14 @@ export function fullUrl(slug: string, trailingSlash: boolean = false): string {
return new URL(s, url).href
}

// FIXME: messy code (I'm tired to fight wiht TS type)
// FIXME
export function sluggize(
slug: Array<string> | string, // NOTE: Next.js request context seems <any>.
prefix: string | undefined = undefined,
fallback: string | undefined = undefined
) {
try {
if (slug instanceof Array) {
const p = prefix === undefined ? undefined : '/' + prefix;
const s = slug as Array<string>;
const arr = [p].concat(s);
return arr.join('/').replace(/\/{2,}/g, '/');
} else {
return slug;
}
} catch {
return fallback;
if (slug instanceof Array) {
return slug.join('/').replace(/\/{2,}/g, '/');
} else {
return slug;
}
}

Expand Down

0 comments on commit 433b68d

Please sign in to comment.