Finally! Using an Astro API Route to handle all the queries which works like a dream. Also created a custom function to get the relative path for image files, fixing any external access issues. Co-authored-by: june <self@breadone.net> Co-committed-by: june <self@breadone.net>
This commit was merged in pull request #7.
This commit is contained in:
@@ -5,7 +5,7 @@ import TagRow from "./TagRow.astro"
|
||||
const { recipe } = Astro.props;
|
||||
|
||||
const headerImage = await client.collection("images").getOne(recipe.images[0])
|
||||
const image = await client.files.getURL(headerImage, headerImage.image)
|
||||
const image = await client.files.getRelativeURL(headerImage, headerImage.image)
|
||||
---
|
||||
|
||||
<div class="relative z-0 flex h-50">
|
||||
|
||||
@@ -4,7 +4,7 @@ const { class: className, recipe } = Astro.props
|
||||
|
||||
async function getLink(img: string) {
|
||||
const record = await client.collection("images").getOne(img)
|
||||
const link = await client.files.getURL(record, record.image)
|
||||
const link = await client.files.getRelativeURL(record, record.image)
|
||||
return link
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
import Pocketbase from "pocketbase"
|
||||
|
||||
const client = new Pocketbase(import.meta.env.PUBLIC_URL)
|
||||
const client = new Pocketbase("http://localhost:4321")
|
||||
client.autoCancellation(false)
|
||||
|
||||
// Return a relative url for file instead of full path including localhost, which breaks external access
|
||||
client.files.getRelativeURL = (record: { [key: string]: any; }, filename: string, queryParams?: FileOptions | undefined) => {
|
||||
const res = client.files.getURL(record, filename)
|
||||
return res.substring(21)
|
||||
}
|
||||
|
||||
export default client;
|
||||
16
src/pages/api/[...proxy].ts
Normal file
16
src/pages/api/[...proxy].ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import type { APIRoute } from "astro";
|
||||
|
||||
// THANK YOU https://stackoverflow.com/a/77297521 !!!!!!!
|
||||
|
||||
const getProxyUrl = (request: Request) => {
|
||||
const proxyUrl = new URL(import.meta.env.PUBLIC_PB_URL);
|
||||
const requestUrl = new URL(request.url);
|
||||
|
||||
return new URL(requestUrl.pathname, proxyUrl);
|
||||
};
|
||||
|
||||
export const ALL: APIRoute = async ({ request }) => {
|
||||
const proxyUrl = getProxyUrl(request);
|
||||
const response = await fetch(proxyUrl.href, request);
|
||||
return new Response(response.body);
|
||||
};
|
||||
Reference in New Issue
Block a user