{
text: 'Server-Side Rendering (SSR)',
link: '/ssr/',
+ children: [
+ {
+ text: 'Vue and Vite',
+ link: '/ssr/',
+ },
+ {
+ text: 'Nuxt.js',
+ link: '/ssr/nuxt.html',
+ },
+ ],
},
{
text: 'Cookbook',
--code-inline-bg-color: var(--c-black-light);
}
+html:not(.light) .custom-block.warning {
+ color: var(--c-text);
+}
+
+html:not(.light) .custom-block.warning a {
+ color: var(--c-brand);
+}
+
html:not(.light) .DocSearch {
--docsearch-text-color: var(--c-white-dark);
--docsearch-container-background: rgba(9, 10, 17, 0.8);
```
:::tip
-`pinia@next` install Pinia v2 for Vue 3. If your app is using Vue 2, you need to install Pinia v1: `pinia@latest` **and** `@vue/composition-api`. If you are using Nuxt, you should follow [these instructions](./ssr/index.md#nuxt-js).
+`pinia@next` install Pinia v2 for Vue 3. If your app is using Vue 2, you need to install Pinia v1: `pinia@latest` **and** `@vue/composition-api`. If you are using Nuxt, you should follow [these instructions](/ssr/nuxt).
:::
Create a pinia (the root store) and pass it to app:
# Server Side Rendering (SSR)
+:::tip
+If you are using **Nuxt.js,** you need to read [**these instructions**](./nuxt.md) instead.
+:::
+
Creating stores with Pinia should work out of the box for SSR as long as you call your `useStore()` functions at the top of `setup` functions, `getters` and `actions`:
```ts
export default defineComponent({
setup() {
- // this works because pinia knows what application is running
+ // this works because pinia knows what application is running inside of
+ // `setup()`
const main = useMainStore()
return { main }
},
})
```
+## Using the store outside of `setup()`
+
If you need to use the store somewhere else, you need to pass the `pinia` instance [that was passed to the app](#install-the-plugin) to the `useStore()` function call:
```ts
app.use(pinia)
router.beforeEach((to) => {
- // ✅ This will work make sure the correct store is used for the current running app
+ // ✅ This will work make sure the correct store is used for the
+ // current running app
const main = useMainStore(pinia)
if (to.meta.requiresAuth && !main.isLoggedIn) return '/login'
})
```
-## Nuxt.js
-
-Make sure to install [`@nuxtjs/composition-api`](https://composition-api.nuxtjs.org/):
-
-```bash
-yarn add pinia @nuxtjs/composition-api
-# or with npm
-npm install pinia @nuxtjs/composition-api
-```
-
-If you are using Nuxt, we supply a _module_ to handle everything for you, you only need to add it to `buildModules` in your `nuxt.config.js` file:
+Pinia conveniently adds itself as `$pinia` to your app so you can use it in functions like `serverPrefetch()`:
```js
-// nuxt.config.js
export default {
- // ... other options
- buildModules: ['@nuxtjs/composition-api', 'pinia/nuxt'],
-}
-```
-
-If you are using TypeScript or have a `jsconfig.json`, you should also add the types for `context.pinia`:
-
-```js
-{
- "include": [
- // ...
- "pinia/nuxt/types"
- ]
+ serverPrefetch() {
+ const store = useStore(this.$pinia)
+ },
}
```
--- /dev/null
+# Nuxt.js
+
+Using Pinia with [Nuxt.js](https://nuxtjs.org/) is easier since Nuxt takes care of a lot of things when it comes to _server side rendering_.
+
+## Installation
+
+Make sure to install [`@nuxtjs/composition-api`](https://composition-api.nuxtjs.org/) alongside `pinia`:
+
+```bash
+yarn add pinia @nuxtjs/composition-api
+# or with npm
+npm install pinia @nuxtjs/composition-api
+```
+
+We supply a _module_ to handle everything for you, you only need to add it to `buildModules` in your `nuxt.config.js` file:
+
+```js
+// nuxt.config.js
+export default {
+ // ... other options
+ buildModules: ['@nuxtjs/composition-api', 'pinia/nuxt'],
+}
+```
+
+And that's it, use your store as usual!
+
+## Using the store outside of `setup()`
+
+If you want to use a store outside of `setup()`, remember to pass the `pinia` object to `useStore()`. We added it to [the context](https://nuxtjs.org/docs/2.x/internals-glossary/context) so you have access to it in `asyncData()` and `fetch()`:
+
+```js
+export default {
+ asyncData({ pinia }) {
+ const store = useStore(pinia)
+ },
+}
+```
+
+## Using the Nuxt context in stores
+
+You can also use [the context](https://nuxtjs.org/docs/2.x/internals-glossary/context) in any store by using the injected property `$nuxt`:
+
+```js
+defineStore({
+ id: 'main',
+
+ actions: {
+ login() {
+ if (!canLogin()) {
+ this.$nuxt.redirect('/login')
+ }
+ },
+ },
+})
+```
+
+## Typescript
+
+If you are using TypeScript or have a `jsconfig.json`, you should also add the types for `context.pinia`:
+
+```js
+{
+ "include": [
+ // ...
+ "pinia/nuxt/types"
+ ]
+}
+```
+
+This will also ensure you have autocompletion 😉 .