-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Server assets cannot be read on the server after build #10594
Comments
Please provide a minimal reproduction in the form of a repository that can be deployed to Vercel to reproduce this issue. |
@s3812497 Hello I am sorry for the delay i think we different times zones, here is the repo
you can clone it. it reproduces the same error. |
This is an expected error because Instead, you should store your assets in import futuraNormal from '$lib/fonts/FuturaPTBook.otf';
import futuraBold from '$lib/fonts/FuturaPTCondBold.otf';
const fonts: TFontDictionary = {
Futura: {
normal: futuraNormal,
bold: futuraBold
}
}; https://stackblitz.com/edit/github-xfpkrg?file=src%2Flib%2Fserver%2Fpdf%2Findex.ts |
@s3812497 Hello Tee Thanks for your help, i tried this and it worked locally but unfortunately it failed on vercel again and this is the error: |
I don't think we're copying the server assets over / resolving the path correctly for some adapters (node, vercel, etc.). So, trying to read them on the server causes the file to not be found. |
@s3812497 Additionally __dirname and __filename don't work |
related #4671 |
I'm a new user but... have you checked the
|
I believe the output is esm so you would need |
@diramazioni @s3812497 this didn't work
|
@ghostdevv @s3812497 this also didn't work |
@s3812497 hello guys is there anything new about this bug? |
@oxneat Sorry about the long silence. I was stuck on this issue for a while but have now found the root cause. Currently, SvelteKit treats server assets no differently from client assets. Hence, we need to add to the builder methods available and modify existing adapters to correctly copy over the server assets to the correct path on the server. In the case of Vercel, all assets currently live in the |
@s3812497 thanks for the feedback |
It seems that this is actually a known issue with a warning in the docs. |
I'm also running into this. I have a wasm file that needs to be included in a +server.ts route. How do I ensure it's included and accessible at runtime? |
See the last paragraph in #10594 (comment) It should be accessible if you can manually copy the files in. |
@s3812497 How do I do that? Do I need to modify the sveltekit built? |
@wesbos That's right. A sort of post-build script. # you can replace your package.json build script with the below
vite build && cp -r .svelte-kit/output/server/_app .vercel/output/functions/fn.func import { join } from "path";
import { FFmpeg } from "@ffmpeg.wasm/main";
import core from "@ffmpeg.wasm/core-mt";
import wasmPathAb from "@ffmpeg.wasm/core-mt/dist/core.wasm?url";
import { env } from "$env/dynamic/private";
import { dev } from "$app/environment";
let wasmPath = join(process.cwd(), wasmPathAb);
// when running `vite preview`, files are served from .svelte-kit
// see https://kit.svelte.dev/docs/building-your-app#preview-your-app
if (!dev && !env.VERCEL) {
wasmPath = join(process.cwd(), '.svelte-kit/output/server', wasmPathAb)
}
export async function GET() {
const ffmpeg = await FFmpeg.create({
core,
coreOptions: {
wasmPath: wasmPath,
},
});
return new Response(wasmPath);
} As a side note, we should be able to copy the server assets correctly when building for split functions (adapter-vercel and adapter-netlify) because we now retain the vite server manifest. Using the manifest, we can know which files belong to which routes. |
@s3812497 Thank you - is the |
I believe so, unless you have split functions configured and have multiple function folders. |
@oxneat have you been able to get this to work for your font-related problem on Vercel? I'm running into the same thing right now. |
No unfortunately |
@oxneat too bad! Do you by any chance have any more ideas for the original problem this thread is about @eltigerchino? |
It's easy enough to update However, I'm struggling to figure out how to easily determine which assets should go with which functions when the The vite manifest maps the server assets to the file paths of the +layout.server and +page.server files in your project folder. So it'd be ideal to copy over only the server assets that will be used by the single server layout or multiple nested server layouts and the page server load. We should also take into account when a layout breaks out of the nested hierarchy. I'm not sure if this will create too much overhead for the adapter and slow it down. It'd be easier to copy over the same server assets to every serverless function but that would make your build very large and so is one of the reasons this was not implemented earlier. |
Still banging my head against the wall with this. Ended up having to copy + paste the files into the vercel output after build: #!zx
const LOCAL_BUILD_PATH = '.svelte-kit/output/server/';
const VERCEL_BUILD_BASE_PATH = '.vercel/output/functions';
const vercelFuncDirs = (await $`ls -1d ${VERCEL_BUILD_BASE_PATH}/*.func`).stdout
.split('\n')
.map((v) => v.trim())
.filter((v) => v !== '');
const outputDirs = [...vercelFuncDirs, LOCAL_BUILD_PATH];
for (const outputDir of outputDirs) {
// We have to explicitly copy the WASM files to the output directory of each serverless function because Sveltekit + Vercel have no way of knowing that they need to be copied via static analysis.
console.log(`Copying WASM files to ${outputDir}.`);
await $`cp node_modules/@ffmpeg.wasm/core-mt/dist/core.wasm ${outputDir}`;
await $`cp node_modules/@ffmpeg.wasm/core-mt/dist/core.worker.js ${outputDir}/core.worker.cjs`;
await $`cp -r ./src/server/transcripts/audio ${outputDir}`;
} Everyone is saying "simply reference the file in your script!" but it doesn't work. I've tried so many combos of Path.join() and fs.readFile |
This wouldn’t work because the files still need to be copied specifically to the serverless function. The adapter isn’t doing this at the moment. I have a PR ready but my only concern is that it copies all referenced assets such as images and fonts, etc that end up in the serverless function bundle, increasing the size (maybe hitting limits? Slowing down cold starts?). Maybe there should be a way to filter the files included? #10979 |
closed by #10979 |
Reopening because there still some cleanup to do |
In 2.4.0 we added a new Note that the demo uses For now I'll treat this issue as fixed. Thanks! |
@eltigerchino @Rich-Harris so we can now simply reference the file and it will be included in the build? We have a dependency that just assumes the .wasm file exists alongside it, no way to pass it in, so we have to do this post-build pre-deploy script: https://github.com/syntaxfm/website/blob/main/why_do_i_need_this.mjs#L14 |
Describe the bug
Sveletekit PdfMake font import works fine locally but crushes when deployed to vercel
Reproduction
this is the code:
i also created a fonts folder a the same place where the file exists and i imported the fonts using
import normal from "./fonts/FuturaPTBook.otf";
and i did this:but it worked locally and it crashed on vercel
Logs
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: