Skip to content

Commit

Permalink
Add unhead package. Add meta for spa and ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
jlpereira committed Apr 21, 2023
1 parent 9bb9bbc commit 757976f
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 23 deletions.
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title><%- project_name %></title>
<!--head-tags-->
<!--preload-links-->
<!--app-state-->
</head>
<body>
<!--body-tags-open-->
<div id="app"></div>
<script
type="module"
src="/src/entry-client.js"
></script>
<!--body-tags-->
</body>
</html>
132 changes: 132 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
"dependencies": {
"@geoman-io/leaflet-geoman-free": "^2.14.2",
"@nuxt/devalue": "^2.0.0",
"@unhead/ssr": "^1.1.26",
"axios": "^1.3.6",
"js-yaml": "^4.1.0",
"leaflet": "^1.9.3",
"pinia": "^2.0.35",
"unhead": "^1.1.26",
"vue": "^3.2.47",
"vue-router": "^4.1.6"
},
Expand Down
8 changes: 7 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,17 @@ export async function createServer(
render = (await import('./dist/server/entry-server.js')).render
}

const [appHtml, appState, preloadLinks] = await render(url, manifest)
const [appHtml, appState, preloadLinks, tagMeta] = await render(
url,
manifest
)

const html = template
.replace(`<!--preload-links-->`, preloadLinks)
.replace(`<!--app-state-->`, appState)
.replace(`<!--head-tags-->`, tagMeta.headTags)
.replace(`<!--body-tags-open-->`, tagMeta.bodyTagsOpen)
.replace(`<!--body-tags-->`, tagMeta.bodyTags)
.replace(makeAppContainer(), makeAppContainer(appHtml))

res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
Expand Down
9 changes: 9 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@

<script setup>
import ApplicationLayout from '@/layout/Application.vue'
import { useHead, createHead } from 'unhead'
import { getMetaFromConfig } from '@/utils'
const head = createHead()
const meta = getMetaFromConfig(__APP_ENV__)
useHead({
title: __APP_ENV__.project_name,
meta
})
</script>
6 changes: 4 additions & 2 deletions src/entry-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { basename } from 'node:path'
import { renderToString } from 'vue/server-renderer'
import { createApp } from './main'
import { registerGlobalComponents } from '@/components/globalComponents'
import { getActiveHead } from 'unhead'
import { renderSSRHead } from '@unhead/ssr'
import devalue from '@nuxt/devalue'

export async function render(url, manifest) {
Expand All @@ -18,7 +20,7 @@ export async function render(url, manifest) {
// components that have been instantiated during this render call.
const ctx = {}
const html = await renderToString(app, ctx)

const headPayload = await renderSSRHead(getActiveHead())
const renderState = `
<script>
window.initialState = ${devalue(store.state.value)}
Expand All @@ -28,7 +30,7 @@ export async function render(url, manifest) {
// which we can then use to determine what files need to be preloaded for this
// request.
const preloadLinks = renderPreloadLinks(ctx.modules, manifest)
return [html, renderState, preloadLinks]
return [html, renderState, preloadLinks, headPayload]
}

function renderPreloadLinks(modules, manifest) {
Expand Down
19 changes: 14 additions & 5 deletions src/modules/otus/views/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ import { useRoute, useRouter } from 'vue-router'
import { useOtuStore } from '../store/store'
import Breadcrumb from '../components/Breadcrumb/Breadcrumb.vue'
import TaxaInfo from '../components/TaxaInfo.vue'
import { useHead, createHead } from 'unhead'
//import useChildrenRoutes from '../composables/useChildrenRoutes'
const route = useRoute()
Expand All @@ -84,24 +86,31 @@ const taxon = computed(() => store.taxon)
const isReady = computed(() => otu.value?.id && taxon.value?.id)
onServerPrefetch(async () => {
await store.loadInit(route.params.id)
await loadInitialData()
})
watch(
() => route.fullPath,
async () => {
store.$reset()
store.loadInit(route.params.id)
loadInitialData()
}
)
onMounted(() => {
if (!otu.value || otu.value.id !== Number(route.params.id)) {
store.$reset()
store.loadInit(route.params.id)
loadInitialData()
}
})
async function loadInitialData() {
store.$reset()
await store.loadInit(route.params.id)
useHead({
title: `${__APP_ENV__.project_name} - ${taxon.value.full_name}`
})
}
function loadOtu({ id }) {
router.push({
name: 'otus-id-overview',
Expand Down
11 changes: 0 additions & 11 deletions src/plugins/htmlPlugin.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/utils/getMeta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function getMetaFromConfig(configObj) {
const metaObj = Object.entries(configObj).filter(([key, value]) =>
key.startsWith('meta_')
)

return metaObj.map(([key, value]) => {
const metaKey = key.slice(5)

return {
[metaKey]: value
}
})
}
4 changes: 4 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './color'
export * from './getMeta'
export * from './request'
export * from './strings'
4 changes: 1 addition & 3 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { defineConfig } from 'vite'
import { loadConfiguration } from './src/utils/loadConfiguration.js'
import path from 'path'
import htmlPlugin from './src/plugins/htmlPlugin.js'
import Vue from '@vitejs/plugin-vue'
import Markdown from 'vite-plugin-md'
import Pages from 'vite-plugin-pages'
Expand Down Expand Up @@ -41,8 +40,7 @@ export default () => {
return route
}
}
}),
htmlPlugin(configuration)
})
]
})
}

0 comments on commit 757976f

Please sign in to comment.