From 8c6e2f1ac55f7e8ae9d306206451edc03849fc65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADcholas=20Andr=C3=A9?= Date: Fri, 21 Jul 2023 22:54:47 -0300 Subject: [PATCH] feat: check for matches --- .../strategies/PostOrPostsFetchStrategy.ts | 86 ++++++++++--------- .../next/src/data/hooks/usePostOrPosts.ts | 1 - .../blog/{[...path].js => [[...path]].js} | 0 3 files changed, 45 insertions(+), 42 deletions(-) rename projects/wp-nextjs/src/pages/blog/{[...path].js => [[...path]].js} (100%) diff --git a/packages/core/src/data/strategies/PostOrPostsFetchStrategy.ts b/packages/core/src/data/strategies/PostOrPostsFetchStrategy.ts index 9402f2f0d..fc8bc1004 100644 --- a/packages/core/src/data/strategies/PostOrPostsFetchStrategy.ts +++ b/packages/core/src/data/strategies/PostOrPostsFetchStrategy.ts @@ -44,6 +44,8 @@ export class PostOrPostsFetchStrategy< P extends PostOrPostsParams, R extends PostOrPostsFetchStrategyResult, > extends AbstractFetchStrategy { + urlParams: Partial

= {}; + postStrategy: SinglePostFetchStrategy = new SinglePostFetchStrategy(this.baseURL); postsStrategy: PostsArchiveFetchStrategy = new PostsArchiveFetchStrategy(this.baseURL); @@ -53,15 +55,12 @@ export class PostOrPostsFetchStrategy< } getParamsFromURL(path: string, params: Partial

= {}): Partial

{ - console.log( - 'PostsOrPosts - path', - path, - this.postStrategy.getParamsFromURL(path, params.single), - ); - return { + this.urlParams = { single: this.postStrategy.getParamsFromURL(path, params.single), archive: this.postsStrategy.getParamsFromURL(path, params.archive), } as P; + + return this.urlParams; } async fetcher( @@ -69,25 +68,29 @@ export class PostOrPostsFetchStrategy< params: Partial

, options?: Partial, ): Promise> { - if (params.priority === 'single') { - try { - const results = await this.postStrategy.fetcher( - this.postStrategy.buildEndpointURL(params.single ?? {}), - params.single ?? {}, - options, - ); + const didMatchSingle = Object.keys(this.urlParams?.single ?? {}).length > 0; + const didMatchArchive = Object.keys(this.urlParams?.archive ?? {}).length > 0; - return { - ...results, - result: { - isArchive: false, - isSingle: true, - data: results.result, - } as R, - }; - } catch (e) { - console.log(e); - // do nothing + if (params.priority === 'single') { + if (didMatchSingle) { + try { + const results = await this.postStrategy.fetcher( + this.postStrategy.buildEndpointURL(params.single ?? {}), + params.single ?? {}, + options, + ); + + return { + ...results, + result: { + isArchive: false, + isSingle: true, + data: results.result, + } as R, + }; + } catch (e) { + // do nothing + } } // TODO: capture potentiall error and throw a better error message @@ -108,24 +111,25 @@ export class PostOrPostsFetchStrategy< }; } - try { - const results = await this.postsStrategy.fetcher( - this.postsStrategy.buildEndpointURL(params.archive ?? {}), - params.archive ?? {}, - options, - ); + if (didMatchArchive) { + try { + const results = await this.postsStrategy.fetcher( + this.postsStrategy.buildEndpointURL(params.archive ?? {}), + params.archive ?? {}, + options, + ); - return { - ...results, - result: { - isArchive: true, - isSingle: false, - data: results.result, - } as R, - }; - } catch (e) { - console.log('archive not found', e); - // do nothing + return { + ...results, + result: { + isArchive: true, + isSingle: false, + data: results.result, + } as R, + }; + } catch (e) { + // do nothing + } } const results = await this.postStrategy.fetcher( diff --git a/packages/next/src/data/hooks/usePostOrPosts.ts b/packages/next/src/data/hooks/usePostOrPosts.ts index 734ce0373..2e11cabb8 100644 --- a/packages/next/src/data/hooks/usePostOrPosts.ts +++ b/packages/next/src/data/hooks/usePostOrPosts.ts @@ -23,7 +23,6 @@ export function usePostOrPosts< ) { const useFetchArguments = usePrepareFetch(params, options); - console.log('usePostOrPosts', useFetchArguments); return useFetchPostOrPosts( useFetchArguments.params, useFetchArguments.options, diff --git a/projects/wp-nextjs/src/pages/blog/[...path].js b/projects/wp-nextjs/src/pages/blog/[[...path]].js similarity index 100% rename from projects/wp-nextjs/src/pages/blog/[...path].js rename to projects/wp-nextjs/src/pages/blog/[[...path]].js