From 433b68d524687c078fca2c7d8282a356d58e908e Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Sat, 27 Jan 2024 00:51:06 +0900 Subject: [PATCH] refactor(url/slug): delete unnecessary args --- __tests__/unit/utils/slug.test.ts | 22 ++++------------------ src/app/articles/[...slug]/page.tsx | 4 ++-- src/utils/url.ts | 19 +++++-------------- 3 files changed, 11 insertions(+), 34 deletions(-) diff --git a/__tests__/unit/utils/slug.test.ts b/__tests__/unit/utils/slug.test.ts index 2810a11..3620595 100644 --- a/__tests__/unit/utils/slug.test.ts +++ b/__tests__/unit/utils/slug.test.ts @@ -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'); -}); -*/ diff --git a/src/app/articles/[...slug]/page.tsx b/src/app/articles/[...slug]/page.tsx index 06adda8..5f7bada 100644 --- a/src/app/articles/[...slug]/page.tsx +++ b/src/app/articles/[...slug]/page.tsx @@ -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 }}): Promise { - 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); } @@ -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 = { diff --git a/src/utils/url.ts b/src/utils/url.ts index 34ee599..9eb58c8 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -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, // NOTE: Next.js request context seems . - prefix: string | undefined = undefined, - fallback: string | undefined = undefined ) { - try { - if (slug instanceof Array) { - const p = prefix === undefined ? undefined : '/' + prefix; - const s = slug as Array; - 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; } }