From 50e8530e962f4dcb350c53b72a84370744ba1171 Mon Sep 17 00:00:00 2001 From: Pacharapol Withayasakpunt Date: Thu, 17 Dec 2020 18:51:26 +0700 Subject: [PATCH] fix canonical url --- .git-hooks/precommit.sh | 4 -- packages/nuxt/pages/post/_.vue | 63 +++++++++++++++++++++++++++++- packages/nuxt/plugins/axios-fix.ts | 7 ++-- packages/nuxt/scripts/make-html.ts | 2 +- packages/nuxt/server/cli.ts | 4 +- 5 files changed, 69 insertions(+), 11 deletions(-) delete mode 100644 .git-hooks/precommit.sh diff --git a/.git-hooks/precommit.sh b/.git-hooks/precommit.sh deleted file mode 100644 index a6b495d..0000000 --- a/.git-hooks/precommit.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/zsh - -# current directory is always `.git/..` root -# echo $PWD diff --git a/packages/nuxt/pages/post/_.vue b/packages/nuxt/pages/post/_.vue index 3ecf94f..0bb8aa6 100644 --- a/packages/nuxt/pages/post/_.vue +++ b/packages/nuxt/pages/post/_.vue @@ -59,6 +59,65 @@ import '~/assets/remark42' post: r, } }, + head() { + const { title: _title, content, tag, image } = this.post + const title = `${_title} - ${THEME.title}` + const description = content.substr(0, 140) + + return { + title, + meta: [ + { + hid: 'description', + name: 'description', + content: description, + }, + ...(tag + ? [ + { + hid: 'keywords', + name: 'keywords', + content: tag.join(','), + }, + ] + : []), + { + hid: 'og:title', + property: 'og:title', + content: title, + }, + { + hid: 'og:description', + property: 'og:description', + content: description, + }, + { + hid: 'twitter:title', + property: 'twitter:title', + content: title, + }, + { + hid: 'twitter:description', + property: 'twitter:description', + content: description, + }, + ...(image + ? [ + { + hid: 'og:image', + property: 'og:image', + content: image, + }, + { + hid: 'twitter:image', + property: 'twitter:image', + content: image, + }, + ] + : []), + ], + } + }, mounted() { this.initRemark42() }, @@ -69,14 +128,14 @@ import '~/assets/remark42' }, }) export default class PostPage extends Vue { - post: { + post!: { date?: string title: string image?: string tag?: string[] category?: string[] content: string - } | null = null + } theme = THEME diff --git a/packages/nuxt/plugins/axios-fix.ts b/packages/nuxt/plugins/axios-fix.ts index b5d85c1..9169656 100644 --- a/packages/nuxt/plugins/axios-fix.ts +++ b/packages/nuxt/plugins/axios-fix.ts @@ -1,9 +1,10 @@ import { Plugin } from '@nuxt/types' const plugin: Plugin = ({ $axios, error }) => { - if (typeof location !== 'undefined') { - $axios.defaults.baseURL = location.origin - } + $axios.defaults.baseURL = + typeof location !== 'undefined' + ? location.origin + : `http://localhost:${process.env.SERVER_PORT}` $axios.interceptors.response.use( (r) => { diff --git a/packages/nuxt/scripts/make-html.ts b/packages/nuxt/scripts/make-html.ts index 4ae0515..cc9f2ab 100644 --- a/packages/nuxt/scripts/make-html.ts +++ b/packages/nuxt/scripts/make-html.ts @@ -108,7 +108,7 @@ export class MakeHtml { return { html: this.$('
').append($div).html() || '', - text: $div.text(), + text: $div.text().trim(), } } diff --git a/packages/nuxt/server/cli.ts b/packages/nuxt/server/cli.ts index b90492e..9a44bee 100644 --- a/packages/nuxt/server/cli.ts +++ b/packages/nuxt/server/cli.ts @@ -14,7 +14,9 @@ export async function runServer() { await initDatabase() const app = express() - app.use(morgan('common')) + if (process.env.NODE_ENV === 'development') { + app.use(morgan('common')) + } const router = express.Router() app.use('/api', router)