fix: include server load data in universal load for recipe page title
The +page.server.ts fetches recipe data and strips HTML tags server-side to avoid bundling cheerio in the client. However, the universal load in +page.ts wasn't including this data in its return value. Fixed by: 1. Having +page.server.ts fetch the recipe directly (since it runs before +page.ts and can't access its data via parent()) 2. Adding the `data` parameter to +page.ts and spreading it in the return Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,24 @@
|
|||||||
import { redirect, error } from '@sveltejs/kit';
|
import { redirect, error } from '@sveltejs/kit';
|
||||||
import { stripHtmlTags } from '$lib/js/stripHtmlTags';
|
import { stripHtmlTags } from '$lib/js/stripHtmlTags';
|
||||||
|
|
||||||
export async function load({ parent }) {
|
export async function load({ params, fetch }) {
|
||||||
// Get data from universal load function
|
// Fetch recipe data to strip HTML tags server-side
|
||||||
const data = await parent();
|
// This avoids bundling cheerio in the client bundle
|
||||||
|
const isEnglish = params.recipeLang === 'recipes';
|
||||||
|
const apiBase = isEnglish ? '/api/recipes' : '/api/rezepte';
|
||||||
|
|
||||||
// Strip HTML tags server-side to avoid bundling cheerio in client
|
const res = await fetch(`${apiBase}/items/${params.name}`);
|
||||||
const strippedName = stripHtmlTags(data.name);
|
if (!res.ok) {
|
||||||
const strippedDescription = stripHtmlTags(data.description);
|
// Let the universal load function handle the error
|
||||||
|
return {
|
||||||
|
strippedName: '',
|
||||||
|
strippedDescription: '',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = await res.json();
|
||||||
|
const strippedName = stripHtmlTags(item.name);
|
||||||
|
const strippedDescription = stripHtmlTags(item.description);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
strippedName,
|
strippedName,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { error } from "@sveltejs/kit";
|
import { error } from "@sveltejs/kit";
|
||||||
import { generateRecipeJsonLd } from '$lib/js/recipeJsonLd';
|
import { generateRecipeJsonLd } from '$lib/js/recipeJsonLd';
|
||||||
|
|
||||||
export async function load({ fetch, params, url}) {
|
export async function load({ fetch, params, url, data }) {
|
||||||
const isEnglish = params.recipeLang === 'recipes';
|
const isEnglish = params.recipeLang === 'recipes';
|
||||||
const apiBase = isEnglish ? '/api/recipes' : '/api/rezepte';
|
const apiBase = isEnglish ? '/api/recipes' : '/api/rezepte';
|
||||||
|
|
||||||
@@ -119,6 +119,7 @@ export async function load({ fetch, params, url}) {
|
|||||||
const germanShortName = isEnglish ? (item.germanShortName || '') : item.short_name;
|
const germanShortName = isEnglish ? (item.germanShortName || '') : item.short_name;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
...data, // Include server load data (strippedName, strippedDescription)
|
||||||
...item,
|
...item,
|
||||||
isFavorite,
|
isFavorite,
|
||||||
multiplier,
|
multiplier,
|
||||||
|
|||||||
Reference in New Issue
Block a user