-
-
Notifications
You must be signed in to change notification settings - Fork 978
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support raw Vue SSR * test: warn clients on ssrPlugin install * refactor: remove unnecessary branch * test: ignore coverage on path not reachable without modifying serverPrefetch option merge * refactor: simulate node-environment with jest * refactor: adjust warn message for wrong environment
- Loading branch information
1 parent
6f207ce
commit 91d7b38
Showing
9 changed files
with
129 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,16 @@ | ||
import Vue from 'vue' | ||
// import VueCompositionApi from '@vue/composition-api' | ||
import App from './App' | ||
import { useStore } from './store' | ||
import { setActiveReq } from '../../../src' | ||
|
||
// Done in setup.ts | ||
// Vue.use(VueCompositionApi) | ||
|
||
export function createApp() { | ||
// create router and store instances | ||
setActiveReq({}) | ||
const store = useStore() | ||
|
||
store.state.counter++ | ||
|
||
// create the app instance, injecting both the router and the store | ||
const app = new Vue({ | ||
render: h => h(App), | ||
}) | ||
|
||
// expose the app, the router and the store. | ||
return { app, store } | ||
return { app } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Vue from 'vue' | ||
import { PiniaSsr } from '../../src' | ||
|
||
it('should warn when installed in the browser', () => { | ||
const mixinSpy = jest.spyOn(Vue, 'mixin') | ||
const warnSpy = jest.spyOn(console, 'warn') | ||
Vue.use(PiniaSsr) | ||
expect(warnSpy).toHaveBeenCalledWith( | ||
expect.stringMatching(/seems to be used in the browser bundle/i) | ||
) | ||
expect(mixinSpy).not.toHaveBeenCalled() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { createStore } from './store' | ||
export { setActiveReq, setStateProvider, getRootState } from './rootStore' | ||
export { StateTree, StoreGetter, Store } from './types' | ||
export { PiniaSsr } from './ssrPlugin' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { VueConstructor } from 'vue/types' | ||
import { setActiveReq } from './rootStore' | ||
|
||
export const PiniaSsr = (vue: VueConstructor) => { | ||
const isServer = typeof window === 'undefined' | ||
|
||
if (!isServer) { | ||
console.warn( | ||
'`PiniaSsrPlugin` seems to be used in the browser bundle. You should only call it on the server entry: https://github.com/posva/pinia#raw-vue-ssr' | ||
) | ||
return | ||
} | ||
|
||
vue.mixin({ | ||
beforeCreate() { | ||
const { setup, serverPrefetch } = this.$options | ||
if (setup) { | ||
this.$options.setup = (props, context) => { | ||
// @ts-ignore | ||
setActiveReq(context.ssrContext.req) | ||
return setup(props, context) | ||
} | ||
} | ||
|
||
if (serverPrefetch) { | ||
const patchedServerPrefetch = Array.isArray(serverPrefetch) | ||
? serverPrefetch.slice() | ||
: // serverPrefetch not being an array cannot be triggered due tue options merge | ||
// https://github.com/vuejs/vue/blob/7912f75c5eb09e0aef3e4bfd8a3bb78cad7540d7/src/core/util/options.js#L149 | ||
/* istanbul ignore next */ | ||
[serverPrefetch] | ||
|
||
for (let i = 0; i < patchedServerPrefetch.length; i++) { | ||
const original = patchedServerPrefetch[i] | ||
patchedServerPrefetch[i] = function() { | ||
// @ts-ignore | ||
setActiveReq(this.$ssrContext.req) | ||
|
||
return original.call(this) | ||
} | ||
} | ||
|
||
// @ts-ignore | ||
this.$options.serverPrefetch = patchedServerPrefetch | ||
} | ||
}, | ||
}) | ||
} |