Skip to content

Commit 02112e1

Browse files
committed
feat: add modern option, fixes #134
1 parent 9433768 commit 02112e1

File tree

10 files changed

+55
-7
lines changed

10 files changed

+55
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ yarn add @nuxtjs/prismic # or npm install @nuxtjs/prismic
3636
'@nuxtjs/prismic',
3737
],
3838
prismic: {
39-
endpoint: 'https://<REPOSITORY>.cdn.prismic.io/api/v2'
39+
endpoint: 'https://<REPOSITORY>.cdn.prismic.io/api/v2',
40+
modern: true
4041
// see documentation for more!
4142
}
4243
}

docs/content/en/1.index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Introduction
33
description: 'Easily connect your Nuxt.js application to your content hosted on Prismic'
44
category: 'Getting Started'
5-
version: 1.3
5+
version: 1.2
66
fullscreen: false
77
menu: true
88
menuTitle: Introduction

docs/content/en/2.installation.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,19 @@ Then, add `@nuxtjs/prismic` to the `buildModules` section of `nuxt.config.js` an
3737
'@nuxtjs/prismic'
3838
],
3939
prismic: {
40-
endpoint: 'https://<REPOSITORY>.cdn.prismic.io/api/v2'
40+
endpoint: 'https://<REPOSITORY>.cdn.prismic.io/api/v2',
41+
modern: true
4142
/* see configuration for more */
4243
}
4344
}
4445
```
4546

47+
48+
> The `modern` option allows the module to use future-proof features. If you're new to the module you should leave it on and not worry about it.
49+
>
50+
> More on the `modern` option in the [configuration section](./configuration#modern).
51+
52+
4653
Finally, you're expected to provide a link resolver function at `~/app/prismic/link-resolver.js`:
4754

4855
```javascript[link-resolver.js]

docs/content/en/3.injected-kits.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Injected Kits
33
description: 'Easily connect your Nuxt.js application to your content hosted on Prismic'
44
category: 'Getting Started'
5-
version: 1.3
5+
version: 1.2
66
fullscreen: false
77
menu: true
88
menuTitle: Injected Kits

docs/content/en/6.configuration.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Configuration
33
description: 'Easily connect your Nuxt.js application to your content hosted on Prismic'
44
category: 'Module API Reference'
5-
version: 1.2
5+
version: 1.3
66
fullscreen: false
77
menu: true
88
menuTitle: Configuration
@@ -153,6 +153,19 @@ prismic: {
153153

154154
Your [html serializer](https://prismic.io/docs/technologies/html-serializer-nuxtjs) function if you need one. By default the module expects you to have it exported at `~/app/prismic/html-serializer.js`.
155155

156+
157+
### modern
158+
<d-badge>v1.3+</d-badge>
159+
160+
- Type `Boolean`
161+
- Default: `false`
162+
163+
This option allows the module to use future-proof features. When set to `true` it:
164+
165+
- Turns off deprecated features unless explicitly enabled (this only impact `disabledGenerator` below, automatically disabling it);
166+
- Tells the module to use the more recent `Prismic.client` method instead of the old `Prismic.api` one when initing the API. This allows the API to lazily init itself once in production, saving your website from useless calls. Although this comes with some minor breaking changes (that won't impact you 99% of the time), [see #134 for more information](https://github.com/nuxt-community/prismic-module/issues/134).
167+
168+
156169
### disableGenerator
157170
<d-badge>Deprecated</d-badge>
158171

@@ -177,7 +190,8 @@ Default configuration only expects you to provide [your Prismic API endpoint](#e
177190
export default {
178191
prismic: {
179192
preview: true,
180-
components: true
193+
components: true,
194+
modern: false
181195
}
182196
}
183197
```

example/nuxt.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export default {
33
'../src/module.js'
44
],
55
prismic: {
6-
endpoint: 'https://200629-sms-hoy.cdn.prismic.io/api/v2'
6+
endpoint: 'https://200629-sms-hoy.cdn.prismic.io/api/v2',
7+
modern: true
78
}
89
}

example/pages/index.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export default {
1414
} else {
1515
error({ statusCode: 404, message: 'Page not found' })
1616
}
17+
},
18+
mounted () {
19+
console.log(this.$prismic.api)
1720
}
1821
}
1922
</script>

src/module.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ function install (moduleOptions) {
77
const options = {
88
preview: true,
99
components: true,
10+
modern: false,
1011
...moduleOptions,
1112
...(this.options.prismic || {})
1213
}
1314
if (options.preview === true) {
1415
options.preview = '/preview'
1516
}
17+
if (options.modern === true && typeof options.disableGenerator === 'undefined') {
18+
options.disableGenerator = true
19+
}
1620
if (!options.endpoint) {
1721
logger.warn('Options `endpoint` is required, disabling module...')
1822
return
@@ -72,6 +76,7 @@ function install (moduleOptions) {
7276
options: {
7377
preview: options.preview,
7478
endpoint: options.endpoint,
79+
modern: options.modern,
7580
apiOptions,
7681
repo,
7782
script: `//static.cdn.prismic.io/prismic.min.js?repo=${repo}&new=true`

templates/plugins/prismic.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export default async (context, inject) => {
1616

1717
let api = {}
1818
try {
19+
<% if (options.modern) { %>
20+
api = Prismic.client('<%= options.endpoint %>', Object.assign({}, options, <%= JSON.stringify(options.apiOptions) %>))
21+
<% } else { %>
1922
api = await Prismic.api('<%= options.endpoint %>', Object.assign({}, options, <%= JSON.stringify(options.apiOptions) %>))
23+
<% } %>
2024
} catch (error) {
2125
console.error(error)
2226
console.error("Failed to init Prismic API, preventing app fatal error.")

test/index.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,17 @@ describe('prismic-nuxt module', () => {
167167
prismicNuxt.call(context, moduleOptions)
168168
expect(context.nuxt.hook).not.toBeCalledWith('generate:before', expect.anything())
169169
})
170+
171+
it('should not run generate if only modern is set', () => {
172+
moduleOptions.modern = true
173+
prismicNuxt.call(context, moduleOptions)
174+
expect(context.nuxt.hook).not.toBeCalledWith('generate:before', expect.anything())
175+
})
176+
177+
it('should run generate if modern is set and generator is explicitely not disabled', () => {
178+
moduleOptions.modern = true
179+
moduleOptions.disableGenerator = false
180+
prismicNuxt.call(context, moduleOptions)
181+
expect(context.nuxt.hook).toBeCalledWith('generate:before', expect.anything())
182+
})
170183
})

0 commit comments

Comments
 (0)