UNPKG

nitropage

Version:

A free and open source, extensible visual page builder based on SolidStart.

43 lines (37 loc) 1.07 kB
import { createAsync, query, useLocation } from "@solidjs/router"; import { sharedConfig } from "solid-js"; import { RequestEvent } from "solid-js/web"; import { getPageVisit as getPageVisit_ } from "../lib/server/page"; import { LOADING } from "../utils"; export const getPageVisit = query( async (args: Parameters<typeof getPageVisit_>[0]) => { "use server"; return await getPageVisit_(args); }, "np-pageVisit", ); /** * Prevents cached SSR data from being used during client navigation * * In PROD environments SSR PageVisit data doesnt include passive elements */ export const isInitial = (event?: RequestEvent) => !!( import.meta.env.PROD && !event?.router?.dataOnly && (import.meta.env.SSR || sharedConfig.context) ); export const usePageVisit = () => { const location = useLocation(); const page = createAsync( () => { const initial = isInitial(); return getPageVisit({ urlPath: location.pathname, initial }); }, { initialValue: LOADING, deferStream: true, }, ); return page; };