kit: Server assets cannot be read on the server after build

Describe the bug

Sveletekit PdfMake font import works fine locally but crushes when deployed to vercel

Reproduction

this is the code:

const fonts: TFontDictionary = {
    Futura: {
        //         FuturaPTBook.otf
        // FuturaPTCondBold.otf
        normal: `./static/fonts/FuturaPTBook.otf`,
        bold: `./static/fonts/FuturaPTBold.otf`
    }
};
const printer = new PdfPrinter(fonts);

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: Screenshot from 2023-08-21 18-09-25

const fonts: TFontDictionary = {
    Futura: {
        //         FuturaPTBook.otf
        // FuturaPTCondBold.otf
        normal: path.join(".",normal),
        bold: path.join(".",bold)
    }
};

but it worked locally and it crashed on vercel

Logs

Error: ENOENT: no such file or directory, open '_app/immutable/assets/FuturaPTCondBold.35d3f580.otf'
    at Object.openSync (node:fs:601:3)
    at Object.readFileSync (node:fs:469:35)
    at PDFFontFactory.open (/var/task/vercel/path0/node_modules/@foliojs-fork/pdfkit/js/pdfkit.js:3384:16)
    at PDFDocument.font (/var/task/vercel/path0/node_modules/@foliojs-fork/pdfkit/js/pdfkit.js:3454:33)
    at FontProvider.provideFont (/var/task/vercel/path0/node_modules/pdfmake/src/fontProvider.js:62:58)
    at /var/task/vercel/path0/node_modules/pdfmake/src/textTools.js:326:27
    at Array.forEach (<anonymous>)
    at measure (/var/task/vercel/path0/node_modules/pdfmake/src/textTools.js:298:13)
    at TextTools.buildInlines (/var/task/vercel/path0/node_modules/pdfmake/src/textTools.js:32:17)
    at DocMeasure.measureLeaf (/var/task/vercel/path0/node_modules/pdfmake/src/docMeasure.js:235:28) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '_app/immutable/assets/FuturaPTCondBold.35d3f580.otf'
}

System Info

System:
    OS: Linux 6.2 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (4) x64 Intel(R) Core(TM) i5-3360M CPU @ 2.80GHz
    Memory: 2.68 GB / 7.68 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    Yarn: 3.6.0 - ~/.nvm/versions/node/v18.16.0/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
    pnpm: 8.6.7 - ~/.nvm/versions/node/v18.16.0/bin/pnpm
  Browsers:
    Chrome: 115.0.5790.170
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.1.0 
    @sveltejs/kit: ^1.20.4 => 1.22.3 
    svelte: ^4.0.5 => 4.0.5 
    vite: ^4.4.2 => 4.4.4

Severity

serious, but I can work around it

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created 10 months ago
  • Comments: 32 (16 by maintainers)

Most upvoted comments

In 2.4.0 we added a new $app/server module with a read function that allows you to solve this problem.

Note that the demo uses pdf-lib rather than pdfmake. AFAICT pdfmake requires you to pass a filepath rather than an array buffer, which isn’t ideal since we want to be as runtime-agnostic as possible. Maybe we could add a function for getting the filepath for an asset to solve those use cases but it wouldn’t make sense in non-Node-like environments (whereas we intend for read to eventually be supported as widely as possible).

For now I’ll treat this issue as fixed. Thanks!