fix: forward the request header when viewing on the /[id] server route

This commit is contained in:
2026-03-30 20:34:30 -04:00
parent e8eb677ba7
commit 62d80597dc
@@ -4,11 +4,11 @@ import { getServerToken } from '$lib/utils/auth';
import { apiUrl, viewInternalApiKey } from '$lib/server/api'; import { apiUrl, viewInternalApiKey } from '$lib/server/api';
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
export const load: PageServerLoad = async ({ params, fetch, url, cookies, parent }) => { export const load: PageServerLoad = async ({ params, fetch, url, cookies, parent, request }) => {
const { id } = params; const { id } = params;
const parentData = await parent().catch(() => null); const parentData = await parent().catch(() => null);
const token = getServerToken(cookies); const token = getServerToken(cookies);
const userAgent = request.headers.get('user-agent') ?? 'Sharpbin Client';
const pasteRes = await fetch(`${apiUrl}/api/paste/${id}`, { const pasteRes = await fetch(`${apiUrl}/api/paste/${id}`, {
headers: { headers: {
@@ -24,7 +24,8 @@ export const load: PageServerLoad = async ({ params, fetch, url, cookies, parent
method: 'POST', method: 'POST',
headers: { headers: {
'X-Internal-API-Key': viewInternalApiKey, 'X-Internal-API-Key': viewInternalApiKey,
Authorization: token ? `Bearer ${token}` : '' Authorization: token ? `Bearer ${token}` : '',
'User-Agent': userAgent
} }
}); });
@@ -32,17 +33,16 @@ export const load: PageServerLoad = async ({ params, fetch, url, cookies, parent
const paste = pasteData.paste as Paste; const paste = pasteData.paste as Paste;
if (viewed.status === 200) { if (viewed.status === 200) {
const viewJson = await viewed.json(); const viewJson = await viewed.json();
if (viewJson.success === true && viewJson.message === 'Paste view recorded.') { if (viewJson.success === true && viewJson.message === 'Paste view recorded.') {
pasteData.views += 1; pasteData.views += 1;
} }
} else { } else if (viewInternalApiKey === '') {
if (viewInternalApiKey === '') {
console.error( console.error(
'The API key for internal requests is not set. Please configure it in the environment variables.' 'The API key for internal requests is not set. Please configure it in the environment variables.'
); );
} }
}
const pasteContent = await fetch(`/api/paste/${id}/raw`); const pasteContent = await fetch(`/api/paste/${id}/raw`);
const pasteOptions = await fetch('/api/paste/info'); const pasteOptions = await fetch('/api/paste/info');