Skip to content

Commit 8c770ff

Browse files
authored
Merge pull request #14 from drunomics/feature/fix-hydration
2 parents 2991883 + 6217d00 commit 8c770ff

File tree

2 files changed

+55
-22
lines changed

2 files changed

+55
-22
lines changed

lib/plugin.js

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class DrupalCe {
1616
*/
1717
this.$currentPage = Vue.observable({
1818
title: null,
19+
statusCode: null,
1920
metadata: {
2021
meta: [],
2122
link: []
@@ -26,13 +27,6 @@ class DrupalCe {
2627
localTasks: null,
2728
settings: {},
2829
})
29-
30-
/**
31-
* Returns an object for rendering as Vue component.
32-
*/
33-
this.$currentPage.contentComponent = () => {
34-
return { name: 'DrupalContent', template: this.$currentPage.content }
35-
}
3630
}
3731

3832
/**
@@ -54,23 +48,49 @@ class DrupalCe {
5448
if (this.options.addRequestContentFormat) {
5549
config.params._content_format = config.params._content_format ?? this.options.addRequestContentFormat
5650
}
57-
const data = await this.$axios.get(path, config).then(res => res.data)
51+
const { data, status } = await this.$axios.get(path, config)
5852
if (!data.title || !data.content) {
5953
return this.context.error({
6054
statusCode: 422,
6155
message: 'Malformed API response. Please make sure to install Custom Elements renderer: https://www.drupal.org/project/lupus_ce_renderer'})
6256
}
63-
this.$currentPage.title = data.title
64-
this.$currentPage.metatags = this.processMetatags(data.metatags)
65-
this.$currentPage.content = data.content
66-
this.$currentPage.breadcrumbs = Array.isArray(data.breadcrumbs) ? data.breadcrumbs : [],
67-
this.$currentPage.messages = this.processMessages(data.messages)
68-
this.$currentPage.localTasks = data.local_tasks
69-
this.$currentPage.settings = data.settings
57+
this.processPageResponse(data, status)
7058
return this.$currentPage
71-
} catch (err) {
72-
this.context.error(err)
73-
return err
59+
} catch (error) {
60+
if (error.response) {
61+
// Request made and server responded. If we get a proper Drupal error
62+
// page, apply it and propagate the error code.
63+
if (error.response.data && error.response.data.title && error.response.data.content) {
64+
this.processPageResponse(error.response.data, error.response.status)
65+
} else {
66+
this.context.error({ statusCode: error.response.status, message: error.message })
67+
}
68+
} else if (error.request) {
69+
// The request was made but no response was received. Usually this
70+
// means a network error.
71+
this.context.error({ statusCode: 503, message: error.message })
72+
} else {
73+
// Some other error happened, so be verbose about it.
74+
this.context.error({ statusCode: 400, message: error.message })
75+
}
76+
}
77+
}
78+
79+
/**
80+
* Updates $currentpage based upon response data.
81+
*/
82+
processPageResponse(data, status) {
83+
this.$currentPage.statusCode = status
84+
this.$currentPage.title = data.title
85+
this.$currentPage.metatags = this.processMetatags(data.metatags)
86+
this.$currentPage.content = data.content
87+
this.$currentPage.breadcrumbs = Array.isArray(data.breadcrumbs) ? data.breadcrumbs : [],
88+
this.$currentPage.messages = this.processMessages(data.messages)
89+
this.$currentPage.localTasks = data.local_tasks
90+
this.$currentPage.settings = data.settings
91+
// When possible, propagate status code (only in server mode).
92+
if (this.context.res) {
93+
this.context.res.statusCode = status
7494
}
7595
}
7696

@@ -133,10 +153,17 @@ export default async function (context, inject) {
133153

134154
const drupal = new DrupalCe($axiosDrupal, context, options)
135155

136-
const { nuxtState = {} } = context || {}
156+
if (process.server) {
157+
context.beforeNuxtRender(({ nuxtState }) => {
158+
// Prepare data for client-side hydration.
159+
nuxtState.drupal = drupal.$currentPage
160+
})
161+
}
137162

138-
if (process.client && !nuxtState.drupal) {
139-
await drupal.fetchPage(context.route.path)
163+
const { nuxtState = {} } = context || {}
164+
// Client-side hydration
165+
if (process.client && nuxtState.drupal) {
166+
drupal.$currentPage = Vue.observable(nuxtState.drupal)
140167
}
141168

142169
inject('drupal', drupal)

scaffold/pages/_.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@
99
1010
export default {
1111
async asyncData ({ route, $drupal }) {
12-
return { page: await $drupal.fetchPage(route.path) }
12+
// Do not return the data here to avoid hydrating data twice. The drupal-ce module is taking care of it already.
13+
await $drupal.fetchPage(route.path)
1314
},
1415
head () {
1516
return {
1617
title: this.page.title,
1718
meta: this.page.metatags.meta,
1819
link: this.page.metatags.link
1920
}
21+
},
22+
computed: {
23+
page () {
24+
return this.$drupal.$currentPage
25+
}
2026
}
2127
}
2228
</script>

0 commit comments

Comments
 (0)