Skip to content

Commit 35a0ca7

Browse files
test workaround
1 parent 8b7016a commit 35a0ca7

4 files changed

Lines changed: 48 additions & 29 deletions

File tree

src/lib/functions/fetchTeiData.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { URL_TEI_PB } from '$lib/constants';
2+
3+
/**
4+
* Fetch verse data for a single witness (codex/fragment) from TEI Publisher.
5+
* @param {string} handle
6+
* @param {string} thirties
7+
* @param {string} verse
8+
* @param {typeof globalThis.fetch} [fetchFn] - optional fetch function (e.g. SvelteKit's fetch)
9+
* @returns {Promise<any>}
10+
*/
11+
export async function fetchWitnessVerse(handle, thirties, verse, fetchFn = fetch) {
12+
const url = `${URL_TEI_PB}/parts/${handle}.xml/json?odd=parzival.odd&view=page&id=${handle}_${thirties}.${verse.replaceAll('I', 'i')}`;
13+
const res = await fetchFn(url);
14+
if (!res.ok) return null;
15+
return res.json();
16+
}
17+
18+
/**
19+
* Fetch verse data for a single Fassung (hyparchetype) from TEI Publisher.
20+
* @param {string} handle
21+
* @param {string} thirties
22+
* @param {string} verse
23+
* @param {typeof globalThis.fetch} [fetchFn] - optional fetch function (e.g. SvelteKit's fetch)
24+
* @returns {Promise<any>}
25+
*/
26+
export async function fetchFassungVerse(handle, thirties, verse, fetchFn = fetch) {
27+
const url = `${URL_TEI_PB}/parts/syn${thirties}.xml/json?odd=parzival.odd&view=single&xpath=//l[@n=%27${handle}%20${thirties}.${verse.replaceAll('I', 'i')}%27]`;
28+
const res = await fetchFn(url);
29+
if (!res.ok) return null;
30+
return res.json();
31+
}

src/routes/einzelverssynopse/[thirties=thirties]/[verse]/+page.server.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { URL_STATIC_API } from '$lib/constants';
22
import { generateEntries } from '$lib/functions/generateEntries';
3+
import { fetchWitnessVerse, fetchFassungVerse } from '$lib/functions/fetchTeiData';
34
import { metadata } from '$lib/data/metadata';
45
import sigilFromHandle from '$lib/functions/sigilFromHandle';
56
import { verses } from '$lib/data/verses';
@@ -92,15 +93,18 @@ export async function load({ fetch, params }) {
9293

9394
const nextVerse = index < filteredVerses.length - 1 ? filteredVerses[index + 1] : null;
9495

95-
/** Helper: fetch and consume the body immediately so no Response body is left unread */
96-
const fetchJson = (/** @type {string} */ url) => fetch(url).then((r) => (r.ok ? r.json() : null));
96+
/**
97+
* Call the TEI Publisher directly (bypasses internal +server.js endpoints
98+
* to avoid SvelteKit prerenderer "Body has already been read" conflict).
99+
* The data endpoints still exist and prerender for client-side use.
100+
*/
97101

98102
// Fetch the textzeugen
99103
const hasSuffix = verseparts[1]; // check if there is a suffix in the URL
100104
await Promise.all(
101105
[...(await metadata).codices, ...(await metadata).fragments].map(
102106
async (/** @type {{ handle: string | number; }} */ element) => {
103-
const handlePath = encodeURIComponent(String(element.handle));
107+
const handle = String(element.handle);
104108
if (hasSuffix) {
105109
const versesToFetch = (await verses).filter(
106110
(v) =>
@@ -110,9 +114,7 @@ export async function load({ fetch, params }) {
110114
);
111115

112116
publisherData[element.handle] = versesToFetch.map((verseObject) => {
113-
return fetchJson(
114-
`/einzelverssynopse/data/${handlePath}/${thirties}/${verseObject.verse}`
115-
);
117+
return fetchWitnessVerse(handle, thirties, verseObject.verse);
116118
});
117119
hasAdditions = true;
118120
} else {
@@ -130,9 +132,7 @@ export async function load({ fetch, params }) {
130132
}
131133

132134
publisherData[element.handle] = versesToFetch.map((verseObject) => {
133-
return fetchJson(
134-
`/einzelverssynopse/data/${handlePath}/${thirties}/${verseObject.verse}`
135-
);
135+
return fetchWitnessVerse(handle, thirties, verseObject.verse);
136136
});
137137
}
138138
}
@@ -143,10 +143,8 @@ export async function load({ fetch, params }) {
143143
if (!hasSuffix) {
144144
(await metadata).hyparchetypes.forEach(
145145
(/** @type {{ handle: string | number; }} */ element) => {
146-
const handlePath = encodeURIComponent(String(element.handle));
147-
publisherData[element.handle] = [
148-
fetchJson(`/einzelverssynopse/data/fassungen/${handlePath}/${thirties}/${verse ?? '01'}`)
149-
];
146+
const handle = String(element.handle);
147+
publisherData[element.handle] = [fetchFassungVerse(handle, thirties, verse ?? '01')];
150148
}
151149
);
152150
}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import { URL_TEI_PB } from '$lib/constants';
21
import { json } from '@sveltejs/kit';
2+
import { fetchWitnessVerse } from '$lib/functions/fetchTeiData';
33

44
/** @type {import('./$types').RequestHandler} */
55
export async function GET({ fetch, params }) {
6-
const res = await fetch(
7-
`${URL_TEI_PB}/parts/${params.handle}.xml/json?odd=parzival.odd&view=page&id=${params.handle}_${params.thirties}.${params.verse.replaceAll('I', 'i')}`
8-
);
9-
10-
const item = await res.json();
11-
12-
return json(item, { status: res.status });
6+
const item = await fetchWitnessVerse(params.handle, params.thirties, params.verse, fetch);
7+
return json(item, { status: item ? 200 : 404 });
138
}
149

1510
export const prerender = true;
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import { URL_TEI_PB } from '$lib/constants';
21
import { json } from '@sveltejs/kit';
2+
import { fetchFassungVerse } from '$lib/functions/fetchTeiData';
33

44
/** @type {import('./$types').RequestHandler} */
55
export async function GET({ fetch, params }) {
6-
const res = await fetch(
7-
`${URL_TEI_PB}/parts/syn${params.thirties}.xml/json?odd=parzival.odd&view=single&xpath=//l[@n=%27${params.handle}%20${params.thirties}.${params.verse.replaceAll('I', 'i')}%27]`
8-
);
9-
10-
const item = await res.json();
11-
12-
return json(item, { status: res.status });
6+
const item = await fetchFassungVerse(params.handle, params.thirties, params.verse, fetch);
7+
return json(item, { status: item ? 200 : 404 });
138
}
149

1510
export const prerender = true;

0 commit comments

Comments
 (0)