-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: more progress on data fetching on app router
- Loading branch information
1 parent
fb075b2
commit 41f73e3
Showing
14 changed files
with
156 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { LOGTYPE, fetchRedirect, getHeadstartWPConfig, log } from '@headstartwp/core'; | ||
import { permanentRedirect, redirect } from 'next/navigation'; | ||
|
||
export async function handleFetchError(error: Error, path = '') { | ||
const { redirectStrategy, sourceUrl, debug } = getHeadstartWPConfig(); | ||
|
||
if (debug?.devMode) { | ||
log(LOGTYPE.INFO, '[handleError] error', error.name, error.message); | ||
} | ||
|
||
if (error.name === 'NotFoundError') { | ||
if (redirectStrategy === '404' && path) { | ||
const redirectObject = await fetchRedirect(path, sourceUrl || ''); | ||
|
||
if (redirectObject.location) { | ||
if ([301, 308].includes(redirectObject.status)) { | ||
permanentRedirect(redirectObject.location); | ||
} else { | ||
redirect(redirectObject.location); | ||
} | ||
} | ||
} | ||
} | ||
|
||
throw error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from '../../data/convertToPath'; | ||
export * from './queries/fetchPostByPath'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { FetchOptions, HeadlessConfig, PostEntity, PostParams, fetchPost } from '@headstartwp/core'; | ||
import { handleFetchError } from '../handleFetchError'; | ||
import { convertToPath } from '../../../data/convertToPath'; | ||
|
||
export async function fetchPostByPath< | ||
T extends PostEntity = PostEntity, | ||
P extends PostParams = PostParams, | ||
>( | ||
path: string | string[], | ||
params: P | {} = {}, | ||
options: Partial<FetchOptions> = {}, | ||
_config: HeadlessConfig | undefined = undefined, | ||
) { | ||
const pathname = Array.isArray(path) ? convertToPath(path) : path; | ||
|
||
try { | ||
const result = await fetchPost<T, P>(params, options, pathname, _config); | ||
|
||
return result; | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
handleFetchError(error, pathname); | ||
} | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './data'; | ||
export * from './type'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type HeadstartWPRoute<Params extends { [k: string]: unknown } = {}> = { | ||
params: { path: string[] }; | ||
} & Params; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
projects/wp-nextjs-app/src/app/(single)/[...path]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { fetchPostByPath, HeadstartWPRoute } from '@headstartwp/next'; | ||
|
||
const Single = async ({ params }: HeadstartWPRoute) => { | ||
const { data } = await fetchPostByPath( | ||
params.path, | ||
{ | ||
params: {}, | ||
}, | ||
{ | ||
headers: { | ||
cache: 'no-store', | ||
}, | ||
}, | ||
); | ||
|
||
return ( | ||
<article> | ||
<h1>{data.post.title.rendered}</h1> | ||
</article> | ||
); | ||
}; | ||
|
||
export default Single; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters