Skip to content

Commit

Permalink
feat: SSR updates & Meta work
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoenescu committed Jul 14, 2018
1 parent 9b9c0cd commit e9301c6
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 41 deletions.
6 changes: 1 addition & 5 deletions build/script.dev.ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,7 @@ function render (req, res) {
if (err) {
return handleError(err)
}
const meta = context.$getMeta()
Object.keys(meta).forEach(token => {
html = html.replace(token, meta[token])
})
res.send(html)
res.send(context.$getMetaHTML(html))
console.log(` ⚡️ ${Date.now() - s}ms - ${req.url}`)
})
}
Expand Down
3 changes: 3 additions & 0 deletions dev/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@

<script>
export default {
meta: {
title: 'Quasar Development'
},
data () {
return {
lang: this.$q.i18n.lang,
Expand Down
2 changes: 1 addition & 1 deletion dev/app.entry-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default ssrContext => {
}

// the Promise should resolve to the app instance so it can be rendered
ssrContext.$getMeta = app.$getMeta(app)
ssrContext.$getMetaHTML = app.$getMetaHTML(app)
resolve(app)
}, reject)
})
Expand Down
21 changes: 1 addition & 20 deletions dev/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,7 @@ export function createApp (ssrContext) {
render: h => h(App)
}

const ctx = { app }

if (ssrContext) {
ctx.ssr = {
req: ssrContext.req,
res: ssrContext.res,
setBodyClasses (cls) {
ssrContext.bodyClasses = cls.join(' ')
},
setHtmlAttrs (attrs) {
const str = []
for (let key in attrs) {
str.push(`${key}=${attrs[key]}`)
}
ssrContext.htmlAttrs = str.join(' ')
}
}
}

Quasar.ssrUpdate(ctx)
Quasar.ssrUpdate({ app, ssr: ssrContext })

return {
app: new Vue(app),
Expand Down
8 changes: 4 additions & 4 deletions dev/ssr.index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html {{ htmlAttrs }} %% Q_HTML_ATTRS %%>
<html {{ Q_HTML_ATTRS }}>
<head>
%% Q_HEAD_TAGS %%
{{ Q_HEAD_TAGS }}
<meta charset="utf-8">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
Expand All @@ -13,8 +13,8 @@
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Material+Icons&Roboto:100,300,400,500,700,900">
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/animate.css@^3.5.2/animate.min.css">
</head>
<body class="demo-site {{ bodyClasses }}" %% Q_BODY_ATTRS %%>
%% Q_BODY_TAGS %%
<body class="demo-site {{ Q_BODY_CLASSES }}" {{ Q_BODY_ATTRS}}>
{{ Q_BODY_TAGS }}
<!--vue-ssr-outlet-->
</body>
</html>
12 changes: 9 additions & 3 deletions src/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ export default {
install ($q, queues, cfg) {
if (isSSR) {
queues.server.push((q, ctx) => {
const update = ctx.ssr.setBodyClasses
if (typeof update === 'function') {
update(getBodyClasses(q.platform, cfg))
const
cls = getBodyClasses(q.platform, cfg),
fn = ctx.ssr.setBodyClasses

if (typeof fn === 'function') {
fn(cls)
}
else {
ctx.ssr.Q_BODY_CLASSES = cls.join(' ')
}
})
return
Expand Down
16 changes: 12 additions & 4 deletions src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ export default {
install ($q, queues, Vue, lang) {
if (isSSR) {
queues.server.push((q, ctx) => {
const fn = ctx.ssr.setHtmlAttrs
if (typeof fn === 'function') {
fn({
const
opt = {
lang: q.i18n.lang,
dir: q.i18n.rtl ? 'rtl' : 'ltr'
})
},
fn = ctx.ssr.setHtmlAttrs

if (typeof fn === 'function') {
fn(opt)
}
else {
ctx.ssr.Q_HTML_ATTRS = Object.keys(opt)
.map(key => `${key}=${opt[key]}`)
.join(' ')
}
})
}
Expand Down
26 changes: 22 additions & 4 deletions src/plugins/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function getHead (meta) {
return output
}

function getServerMeta (app) {
function getServerMeta (app, html) {
const meta = {
title: '',
titleTemplate: null,
Expand All @@ -196,10 +196,11 @@ function getServerMeta (app) {
bodyAttrs: {},
noscripts: {}
}

parseMeta(app, meta)
normalize(meta)

return {
const tokens = {
'%%Q_HTML_ATTRS%%': Object.keys(meta.htmlAttrs)
.filter(name => !['lang', 'dir'].includes(name))
.map(getAttr(meta.htmlAttrs))
Expand All @@ -214,6 +215,12 @@ function getServerMeta (app) {
.join('') +
`<script data-qmeta-init>window.__Q_META__=${delete meta.noscripts && JSON.stringify(meta)}</script>`
}

Object.keys(tokens).forEach(key => {
html = html.replace(key, tokens[key])
})

return html
}

function beforeCreate () {
Expand All @@ -235,13 +242,24 @@ function triggerMeta () {
}

export default {
install ({ Vue }) {
install ({ queues, Vue }) {
if (isSSR) {
Vue.prototype.$getMeta = app => () => getServerMeta(app)
Vue.prototype.$getMetaHTML = app => html => getServerMeta(app, html)
Vue.mixin({ beforeCreate })

queues.server.push((q, ctx) => {
ctx.ssr.Q_HTML_ATTRS += ' %%Q_HTML_ATTRS%%'
Object.assign(ctx.ssr, {
Q_HEAD_TAGS: '%%Q_HEAD_TAGS%%',
Q_BODY_ATTRS: '%%Q_BODY_ATTRS%%',
Q_BODY_TAGS: '%%Q_BODY_TAGS%%'
})
// ssr.Q_BODY_CLASSES
})
}
else {
ssrTakeover = fromSSR

Vue.mixin({
beforeCreate,
created () {
Expand Down
6 changes: 6 additions & 0 deletions src/ssr-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export default function (ctx) {
if (ctx.ssr) {
const q = Object.assign({}, $q)

Object.assign(ctx.ssr, {
Q_HEAD_TAGS: '',
Q_BODY_ATTRS: '',
Q_BODY_TAGS: ''
})

queues.server.forEach(run => {
run(q, ctx)
})
Expand Down

0 comments on commit e9301c6

Please sign in to comment.