Skip to content

Commit

Permalink
fix: return status and other info for ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrosenberg committed Jul 23, 2023
1 parent 829aadd commit f45edf9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
17 changes: 13 additions & 4 deletions lib/extra/tools/ssr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SvelteComponentDev } from 'svelte/internal'

Check failure on line 1 in lib/extra/tools/ssr.js

View workflow job for this annotation

GitHub Actions / Test on NodeJS v14.x

Property 'url' does not exist on type 'string | string[] | PreloadOptions'.
import { preloadUrl } from '../../runtime'
import { preloadUrl } from '../../runtime/index.js'

const polyfillFetch = async () => {
const fetch = await import('node-fetch')
Expand All @@ -18,7 +18,15 @@ const polyfillFetch = async () => {
* Returns a statically rendered Routify app
* @param {(SvelteComponentDev|{default: SvelteComponentDev}) & {load: (url:string)=>Promise<any>}} module App.svelte
* @param {string | string[] | import('../../runtime').PreloadOptions=} urlOrOptions one or multiple urls separated by ";<routerName>="
* @returns
* @returns {Promise<{
* html: string,
* css: { code: string, map: string },
* head: '',
* error: string,
* maxage: number,
* props: Object.<string, string>,
* redirect: string
* }>}
*/
export const renderModule = async (module, urlOrOptions) => {
await polyfillFetch()
Expand All @@ -27,6 +35,7 @@ export const renderModule = async (module, urlOrOptions) => {
const url = urlOrOptions.url || urlOrOptions
const load = module.load ? await module.load(url) : {}

await preloadUrl(urlOrOptions)
return { status: 200, ...(await render()), ...load }
const preloadUrlLoad = await preloadUrl(urlOrOptions)

return { status: 200, ...(await render()), ...load, ...preloadUrlLoad?.flat()[0] }
}
10 changes: 9 additions & 1 deletion lib/extra/vite-plugin/assets/esmRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ const template = readFileSync(new URL('./index.html', import.meta.url), 'utf-8')
/**
* Renders a given path to HTML
* @param {string} path
* @returns {Promise<{html: string, load: any}>}
* @returns {Promise<{
* html: string,
* css: { code: string, map: string },
* head: '',
* error: string,
* maxage: number,
* props: Object.<string, string>,
* redirect: string
* }>}
*/
export const render = async (path = '/') => {
const output = await _render(path)
Expand Down
1 change: 1 addition & 0 deletions lib/runtime/helpers/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const preloadUrlFromUrlPairs = async (urlPairs, routesMap) => {
const routers = await Promise.all(routerPromises)

await Promise.all(routers.map(router => router && router.ready()))
return routers.map(router => router.activeRoute.get().load)
}

/**
Expand Down

0 comments on commit f45edf9

Please sign in to comment.