Skip to content

Commit 4442467

Browse files
committed
feat: use runtimeConfig to avoid building when changing Strapi URL
1 parent a9b789c commit 4442467

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

example/pages/index.vue

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<template>
22
<div>
3-
Works!
3+
<h1>@nuxtjs/strapi</h1>
4+
<h2>$strapi.state</h2>
5+
<pre>{{ $strapi.state }}</pre>
6+
<h2>options.url</h2>
7+
<pre>{{ $strapi.$http._defaults.prefixUrl }}</pre>
48
</div>
59
</template>
610

7-
<script>
8-
export default {
9-
11+
<style scoped>
12+
div {
13+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
1014
}
11-
</script>
15+
</style>

lib/module.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module.exports = async function (moduleOptions) {
1313
}, defaults)
1414

1515
this.options.publicRuntimeConfig = this.options.publicRuntimeConfig || {}
16-
this.options.publicRuntimeConfig.strapiUrl = options.url
16+
this.options.publicRuntimeConfig.strapi = this.options.publicRuntimeConfig.strapi || {}
17+
this.options.publicRuntimeConfig.strapi.url = options.url
1718

1819
this.addPlugin({
1920
src: resolve(__dirname, 'plugin.js'),

lib/plugin.js

+26-24
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ class Strapi extends Hookable {
88
constructor (ctx) {
99
super()
1010

11+
ctx.$config = ctx.$config || {} // fallback for Nuxt < 2.13
12+
const runtimeConfig = ctx.$config.strapi || {}
1113
this.state = Vue.observable({ user: null })
1214

1315
this.$cookies = ctx.app.$cookies
1416
this.$http = ctx.$http.create({})
15-
this.$http.setBaseURL('<%= options.url %>')
17+
this.$http.setBaseURL(runtimeConfig.url || '<%= options.url %>')
1618
this.$http.onError((err) => {
1719
const { response: { data: { message: msg } } } = err
1820

@@ -139,32 +141,32 @@ class Strapi extends Hookable {
139141

140142
export default async function (ctx, inject) {
141143
<%= JSON.stringify(options.entities) %>.forEach((entity) => {
142-
Object.defineProperty(Strapi.prototype, `$${entity}`, {
143-
get: function () {
144-
const that = this
145-
return {
146-
find: function (...args) {
147-
return that.find(entity, ...args)
148-
},
149-
findOne: function (...args) {
150-
return that.findOne(entity, ...args)
151-
},
152-
count: function (...args) {
153-
return that.count(entity, ...args)
154-
},
155-
create: function (...args) {
156-
return that.create(entity, ...args)
157-
},
158-
update: function (...args) {
159-
return that.update(entity, ...args)
160-
},
161-
delete: function (...args) {
162-
return that.delete(entity, ...args)
144+
Object.defineProperty(Strapi.prototype, `$${entity}`, {
145+
get () {
146+
const that = this
147+
return {
148+
find (...args) {
149+
return that.find(entity, ...args)
150+
},
151+
findOne (...args) {
152+
return that.findOne(entity, ...args)
153+
},
154+
count (...args) {
155+
return that.count(entity, ...args)
156+
},
157+
create (...args) {
158+
return that.create(entity, ...args)
159+
},
160+
update (...args) {
161+
return that.update(entity, ...args)
162+
},
163+
delete (...args) {
164+
return that.delete(entity, ...args)
165+
}
163166
}
164167
}
165-
}
168+
})
166169
})
167-
})
168170

169171
const strapi = new Strapi(ctx)
170172

0 commit comments

Comments
 (0)