Skip to content

Commit

Permalink
feat(vue-app): universal fetch (#5028)
Browse files Browse the repository at this point in the history
* pkg(nuxt-start): add node-fetch, unfetch

* pkg(vue-app): add node-fetch, unfetch

* add yarn.lock

* feat(config): _app.fetch options

* feat(builder): add fetch to templateVars

* feat(vue-app): polyfill global with fetch

* feat(fixtures/basic): /api/test

* add fetch example to fixtures

* remove unfetch from nuxt-start

* update template snapshot

* revert extra new line in server.js

* single line if
  • Loading branch information
pi0 authored and Sébastien Chopin committed Feb 14, 2019
1 parent d9a0b5f commit 2015140
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions distributions/nuxt-start/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"dependencies": {
"@nuxt/cli": "2.4.3",
"@nuxt/core": "2.4.3",
"node-fetch": "^2.3.0",
"vue": "^2.6.6",
"vue-meta": "^1.5.8",
"vue-no-ssr": "^1.1.1",
Expand Down
1 change: 1 addition & 0 deletions packages/builder/src/context/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class TemplateContext {
isTest: options.test,
debug: options.debug,
vue: { config: options.vue.config },
fetch: options.fetch,
mode: options.mode,
router: options.router,
env: options.env,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ TemplateContext {
],
"env": "test_env",
"extensions": "test|ext",
"fetch": undefined,
"globalName": "test_global",
"globals": Array [
"globals",
Expand Down
5 changes: 5 additions & 0 deletions packages/config/src/config/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export default () => ({
script: []
},

fetch: {
server: true,
client: true
},

plugins: [],

css: [],
Expand Down
4 changes: 4 additions & 0 deletions packages/config/test/__snapshots__/options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ Object {
"mjs",
"ts",
],
"fetch": Object {
"client": true,
"server": true,
},
"generate": Object {
"concurrency": 500,
"dir": "/var/nuxt/test/dist",
Expand Down
8 changes: 8 additions & 0 deletions packages/config/test/config/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ Object {
"editor": undefined,
"env": Object {},
"extensions": Array [],
"fetch": Object {
"client": true,
"server": true,
},
"generate": Object {
"concurrency": 500,
"dir": "dist",
Expand Down Expand Up @@ -466,6 +470,10 @@ Object {
"editor": undefined,
"env": Object {},
"extensions": Array [],
"fetch": Object {
"client": true,
"server": true,
},
"generate": Object {
"concurrency": 500,
"dir": "dist",
Expand Down
2 changes: 2 additions & 0 deletions packages/vue-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"main": "dist/vue-app.js",
"typings": "types/index.d.ts",
"dependencies": {
"node-fetch": "^2.3.0",
"unfetch": "^4.0.1",
"vue": "^2.6.6",
"vue-meta": "^1.5.8",
"vue-no-ssr": "^1.1.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/vue-app/template/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue'
<% if (fetch.client) { %>import fetch from 'unfetch'<% } %>
import middleware from './middleware.js'
import {
applyAsyncData,
Expand All @@ -22,6 +23,8 @@ import NuxtLink from './components/nuxt-link.<%= router.prefetchLinks ? "client"
Vue.component(NuxtLink.name, NuxtLink)
Vue.component('NLink', NuxtLink)

<% if (fetch.client) { %>if (!global.fetch) { global.fetch = fetch }<% } %>

// Global shared references
let _lastPaths = []
let app
Expand Down
3 changes: 3 additions & 0 deletions packages/vue-app/template/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { stringify } from 'querystring'
import Vue from 'vue'
<% if (fetch.server) { %>import fetch from 'node-fetch'<% } %>
import middleware from './middleware.js'
import { applyAsyncData, getMatchedComponents, middlewareSeries, promisify, urlJoin, sanitizeComponent } from './utils.js'
import { createApp, NuxtError } from './index.js'
Expand All @@ -9,6 +10,8 @@ import NuxtLink from './components/nuxt-link.server.js' // should be included af
Vue.component(NuxtLink.name, NuxtLink)
Vue.component('NLink', NuxtLink)

<% if (fetch.server) { %>if (!global.fetch) { global.fetch = fetch }<% } %>

const debug = require('debug')('nuxt:render')
debug.color = 4 // force blue color

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/basic/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export default {
'~/plugins/dir-plugin',
'~/plugins/inject'
],
serverMiddleware: [
{
path: '/api/test',
handler: (_, res) => res.end('Works!')
}
],
build: {
scopeHoisting: true,
publicPath: '',
Expand Down
34 changes: 34 additions & 0 deletions test/fixtures/basic/pages/fetch.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<template>
<div>
{{ data }}
<button @click="update">
Fetch
</button>
<button @click="reload">
Reload
</button>
</div>
</template>

<script>
const name = process.server ? 'server' : 'client'
const baseURL = 'http://localhost:3000/api'
const getData = () => fetch(`${baseURL}/test`)
.then(r => r.text())
.then(r => r + ` (From ${name})`)
export default {
async asyncData() {
const data = await getData()
return { data }
},
methods: {
async update() {
this.data = await getData()
},
reload() {
window.location.reload()
}
}
}
</script>
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11023,6 +11023,11 @@ umask@^1.1.0:
resolved "https://registry.npmjs.org/umask/-/umask-1.1.0.tgz#f29cebf01df517912bb58ff9c4e50fde8e33320d"
integrity sha1-8pzr8B31F5ErtY/5xOUP3o4zMg0=

unfetch@^4.0.1:
version "4.0.1"
resolved "https://registry.npmjs.org/unfetch/-/unfetch-4.0.1.tgz#8750c4c7497ade75d40387d7dbc4ba024416b8f6"
integrity sha512-HzDM9NgldcRvHVDb/O9vKoUszVij30Yw5ePjOZJig8nF/YisG7QN/9CBXZ8dsHLouXMeLZ82r+Jod9M2wFkEbQ==

unicode-canonical-property-names-ecmascript@^1.0.4:
version "1.0.4"
resolved "https://registry.npmjs.org/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818"
Expand Down

0 comments on commit 2015140

Please sign in to comment.