diff --git a/examples/with-cookies/README.md b/examples/with-cookies/README.md new file mode 100644 index 0000000000..d64ba5270a --- /dev/null +++ b/examples/with-cookies/README.md @@ -0,0 +1,3 @@ +# Hello World with Nuxt.js + +https://nuxtjs.org/examples diff --git a/examples/with-cookies/nuxt.config.js b/examples/with-cookies/nuxt.config.js new file mode 100644 index 0000000000..f5d66e70ce --- /dev/null +++ b/examples/with-cookies/nuxt.config.js @@ -0,0 +1,9 @@ +module.exports = { + head: { + title: 'Nuxt-Cookies', + meta: [ + { content: 'width=device-width,initial-scale=1', name: 'viewport' } + ] + }, + plugins: ['~/plugins/cookies'] +} diff --git a/examples/with-cookies/package.json b/examples/with-cookies/package.json new file mode 100644 index 0000000000..17fc29e968 --- /dev/null +++ b/examples/with-cookies/package.json @@ -0,0 +1,13 @@ +{ + "name": "hello-nuxt", + "dependencies": { + "cookie": "^0.3.1", + "js-cookie": "^2.1.4", + "nuxt": "latest" + }, + "scripts": { + "dev": "nuxt", + "build": "nuxt build", + "start": "nuxt start" + } +} diff --git a/examples/with-cookies/pages/index.vue b/examples/with-cookies/pages/index.vue new file mode 100755 index 0000000000..a095b8eb17 --- /dev/null +++ b/examples/with-cookies/pages/index.vue @@ -0,0 +1,74 @@ + + + + + diff --git a/examples/with-cookies/plugins/cookies.js b/examples/with-cookies/plugins/cookies.js new file mode 100644 index 0000000000..465bfaebda --- /dev/null +++ b/examples/with-cookies/plugins/cookies.js @@ -0,0 +1,19 @@ +import cookie from 'cookie' + +export let cookies + +// Called only on client-side +export const refreshCookies = () => { + cookies = cookie.parse(document.cookie) + return cookies +} + +/* +** Executed by ~/.nuxt/index.js with context given +** This method can be asynchronous +*/ +export default ({ isServer, req }) => { + // We update the cookies variable + // So we can read it into our page with: import { cookies } from '~/plugins/cookies.js' + cookies = cookie.parse(isServer ? req.headers.cookie : document.cookie) +}