-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #115 - feat: a promise returning function to transfer state between server and client
- Loading branch information
Showing
11 changed files
with
184 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
--- | ||
|
||
# ssrPromise | ||
|
||
`ssrPromise` runs a promise on the server and serialises the result as a resolved promise for the client. It needs to be run within the `setup()` function but note that it returns a promise which will require special handling. (For example, you cannot just return a promise from setup and use it in the template.) | ||
|
||
```ts | ||
import { | ||
defineComponent, | ||
onBeforeMount, | ||
ref, | ||
ssrPromise, | ||
} from 'nuxt-composition-api' | ||
|
||
export default defineComponent({ | ||
setup() { | ||
const _promise = ssrPromise(async () => myAsyncFunction()) | ||
const resolvedPromise = ref(null) | ||
|
||
onBeforeMount(async () => { | ||
resolvedPromise.value = await _promise | ||
}) | ||
|
||
return { | ||
// On the server, this will be null until the promise resolves. | ||
// On the client, if server-rendered, this will always be the resolved promise. | ||
resolvedPromise, | ||
} | ||
}, | ||
}) | ||
``` | ||
|
||
::: tip | ||
Under the hood, `ssrPromise` requires a key to ensure that the ref values match between client and server. If you have added `nuxt-composition-api` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `nuxt-composition-api/babel` to your Babel plugins. | ||
::: | ||
|
||
::: warning | ||
At the moment, an `ssrPromise` is only suitable for one-offs, unless you provide your own unique key. See [the `ssrRef` documentation](./ssrRef.md) for more information. | ||
::: |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function validateKey<T>(key?: T): asserts key is T { | ||
if (!key) { | ||
throw new Error( | ||
"You must provide a key. You can have it generated automatically by adding 'nuxt-composition-api/babel' to your Babel plugins." | ||
) | ||
} | ||
} |
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,20 @@ | ||
import { Selector } from 'testcafe' | ||
import { navigateTo, expectOnPage } from './helpers' | ||
|
||
// eslint-disable-next-line | ||
fixture`ssrPromise` | ||
|
||
test('Shows data on ssr-loaded page', async t => { | ||
await navigateTo('/promise') | ||
await expectOnPage('promise-server') | ||
|
||
await t.click(Selector('a').withText('home')) | ||
await t.click(Selector('a').withText('promise')) | ||
await expectOnPage('promise-server') | ||
}) | ||
|
||
test('Shows appropriate data on client-loaded page', async t => { | ||
await navigateTo('/') | ||
await t.click(Selector('a').withText('promise')) | ||
await expectOnPage('promise-client') | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<template> | ||
<blockquote> | ||
<p> | ||
<code>promise-{{ promise }}</code> | ||
</p> | ||
</blockquote> | ||
</template> | ||
|
||
<script> | ||
import { | ||
defineComponent, | ||
onBeforeMount, | ||
ref, | ||
ssrPromise, | ||
} from 'nuxt-composition-api' | ||
import { fetcher } from '../utils' | ||
export default defineComponent({ | ||
setup() { | ||
const _promise = ssrPromise(async () => fetcher(process.server ? 'server' : 'client')) | ||
const promise = ref('') | ||
onBeforeMount(async () => { | ||
promise.value = await _promise | ||
}) | ||
return { | ||
promise, | ||
} | ||
}, | ||
}) | ||
</script> |
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