Functions

useHydration

Returns the isomorphic context object, aptly named hydration context.

Works isomorphically on client and server.

// Vue
import { useHydration } from 'fastify-vite-vue/client'
const ctx = useHydration(config)

// React
import { useHydration } from 'fastify-vite-react/client'
const [ctx, update] = useHydration(config)

In React an update function is also returned because ctx is not reactive like in the Vue version.

useHydration config

The config object passed to useHydration is optional. It can either contain a reference to getPayload, which will cause $payload and $payloadPath to be included in the returned object — or a reference to getData, which will cause $data to be included in the returned object.

See Data Fetching for more info.

useHydration context

reqthe core Fastify request object — only available on the server
replythe core Fastify reply object — only available on the server
fastifythe Fastify server instance — only available on the server
$globalGlobal Data — available isomorphically
$payloadRoute Payload data — available isomorphically
$payloadPath()Route Payload endpoint — available isomorphically
$dataRoute Isomorphic Data — available isomorphically

useFastify

Returns reference to the Fastify server instance, only available in SSR.

import { useFastify } from 'fastify-vite-vue/server'

const fastify = useFastify()

This reference is also included in useHydration's return object as fastify.

useRequest

Returns the core Fastify request object, only available in SSR.

import { useRequest } from 'fastify-vite-vue/server'
import { useRequest } from 'fastify-vite-react/server'
const req = useRequest()

For convenience, here's the API reference taken from the official Fastify documentation:

querythe parsed querystring
bodythe body
paramsthe params matching the URL
headersthe headers
rawthe incoming HTTP request from Node core
idthe request id
logthe logger instance of the incoming request
ipthe IP address of the incoming request
ips

An array of the IP addresses in the X-Forwarded-For header of the incoming request.

Only functional if the trustProxy server option is enabled.

hostnamethe hostname of the incoming request
protocolthe protocol of the incoming request (https or http)
methodthe method of the incoming request
urlthe url of the incoming request
routerMethodthe method defined for the router that is handling the request
routerPaththe path pattern defined for the router that is handling the request
is404true if request is being handled by 404 handler, false if it is not
socketthe underlying connection of the incoming request

This reference is also included in useHydration's return object as req.

useReply

Returns the core Fastify reply object, only available in SSR.

import { useReply } from 'fastify-vite-vue/server'
import { useReply } from 'fastify-vite-react/server'
const reply = useReply()

For convenience, here's the API reference taken from the official Fastify documentation:

code(statusCode)sets the HTTP status code.
statusCodereads and sets the HTTP status code.
header(name, value)sets a response header.
headers(object)sets all the keys of the object as response headers.
getHeader(name)retrieve value of already set header.
getHeaders()gets a shallow copy of all current response headers.
removeHeader(key)remove the value of a previously set header.
hasHeader(name)determine if a header has been set.
type(value)sets the `Content-Type` header.
redirect([code,] dest)redirect to the specified url, the status code is optional (defaults to `302`).
callNotFound()Invokes the custom not found handler.
serialize(payload)Serializes the specified payload using the default JSON serializer or using the custom serializer (if one is set) and returns the serialized payload.
serializer(function)Sets a custom serializer for the payload.
send(payload)Sends the payload to the user, could be a plain text, a buffer, JSON, stream, or an Error object.
sentA boolean value that you can use if you need to know if send has already been called.
rawThe [http.ServerResponse](https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_class_http_serverresponse) from Node core.
logThe logger instance of the incoming request.
requestThe incoming request.
contextaccess to the request's [context property](https://www.fastify.io/docs/latest/Request#Request).

This reference is also included in useHydration's return object as reply.