-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathprismic-configuration.js
45 lines (38 loc) · 1.42 KB
/
prismic-configuration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Prismic from 'prismic-javascript'
import Link from 'next/link'
// -- Prismic API endpoint
// Determines which repository to query and fetch data from
// Configure your site's access point here
export const apiEndpoint = 'https://your-repo-name.prismic.io/api/v2'
// -- Access Token if the repository is not public
// Generate a token in your dashboard and configure it here if your repository is private
export const accessToken = ''
// -- Link resolution rules
// Manages links to internal Prismic documents
// Modify as your project grows to handle any new routes you've made
export const linkResolver = (doc) => {
if (doc.type === 'page') {
return `/${doc.uid}`
}
return '/'
}
// Additional helper function for Next/Link component
export const hrefResolver = (doc) => {
if (doc.type === 'page') {
return `/page?uid=${doc.uid}`
}
return '/'
}
export const customLink = (type, element, content, children, index) => (
<Link key={element.data.id} href={hrefResolver(element.data)} as={linkResolver(element.data)}>
<a>{content}</a>
</Link>
)
let frontClient
export const Client = (req = null) => {
if (!req && frontClient) return frontClient // prevent generate new instance for client side since we don't need the refreshed request object.
else {
const options = Object.assign({}, req ? { req } : {}, accessToken ? { accessToken: accessToken } : {})
return Prismic.client(apiEndpoint, options)
}
}