Skip to content
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

Static Adapter: Does not generate folder and subfolder #2848

Closed
lsabi opened this issue Nov 20, 2021 · 2 comments
Closed

Static Adapter: Does not generate folder and subfolder #2848

lsabi opened this issue Nov 20, 2021 · 2 comments

Comments

@lsabi
Copy link

lsabi commented Nov 20, 2021

Describe the bug

When using a rest endpoint built with kit like the following structure

/api/post/index.js
/api/post/[id].js

in order to catch routes like

/api/post
/api/post/1

and building with the static adapter, only the first one will be generated, while the ones with the same prefix won't.

In the config, the order of pages (config.kit.prerender.entries) changes the behaviour.

That is, if /api/post is set first, at build time, a file /build/api/post will be generated throwing an error when accessing /build/api/post/1.

Conversely, if /api/post/1 come first in the list of pages, at build time the file /build/api/post/1 will be generated and an error will be thrown when accessing /api/post.

If multiple /api/post/11 pages are specified before /api/page in the config, then they will all be generated (i.e. pages for all posts), but the one without id will be missing and throw the error.

Reproduction

Here's the demo app showing the problem

https://github.com/lsabi/my-app

Logs

npm run build

> [email protected] build
> svelte-kit build

vite v2.6.14 building for production...
✓ 34 modules transformed.
.svelte-kit/output/client/_app/assets/svelte-logo-87df40b8.svg                           1.85 KiB
.svelte-kit/output/client/_app/assets/fira-mono-cyrillic-400-normal-046b609f.woff2       9.08 KiB
.svelte-kit/output/client/_app/assets/fira-mono-greek-400-normal-1f8b3a07.woff2          10.48 KiB
.svelte-kit/output/client/_app/assets/fira-mono-greek-ext-400-normal-8659ae46.woff2      7.85 KiB
.svelte-kit/output/client/_app/assets/fira-mono-latin-ext-400-normal-b6331a25.woff2      11.84 KiB
.svelte-kit/output/client/_app/assets/fira-mono-cyrillic-ext-400-normal-b3140dd3.woff2   16.11 KiB
.svelte-kit/output/client/_app/assets/fira-mono-latin-400-normal-a2f9dbe8.woff2          16.44 KiB
.svelte-kit/output/client/_app/assets/fira-mono-all-400-normal-0d19eb5d.woff             75.54 KiB
.svelte-kit/output/client/_app/manifest.json                                             2.43 KiB
.svelte-kit/output/client/_app/pages/__layout.svelte-b31113c6.js                         4.70 KiB / gzip: 1.84 KiB
.svelte-kit/output/client/_app/error.svelte-340c3bac.js                                  1.56 KiB / gzip: 0.75 KiB
.svelte-kit/output/client/_app/pages/index.svelte-10012664.js                            4.12 KiB / gzip: 1.70 KiB
.svelte-kit/output/client/_app/pages/about.svelte-9c5ffbae.js                            2.38 KiB / gzip: 1.09 KiB
.svelte-kit/output/client/_app/start-43ac425b.js                                         18.88 KiB / gzip: 6.93 KiB
.svelte-kit/output/client/_app/assets/start-61d1577b.css                                 0.16 KiB / gzip: 0.15 KiB
.svelte-kit/output/client/_app/pages/todos/index.svelte-afe0d957.js                      5.69 KiB / gzip: 2.36 KiB
.svelte-kit/output/client/_app/assets/pages/about.svelte-bf4528fa.css                    0.11 KiB / gzip: 0.10 KiB
.svelte-kit/output/client/_app/assets/pages/__layout.svelte-a7861b78.css                 4.96 KiB / gzip: 1.55 KiB
.svelte-kit/output/client/_app/assets/pages/index.svelte-c63fe1c6.css                    1.34 KiB / gzip: 0.47 KiB
.svelte-kit/output/client/_app/assets/pages/todos/index.svelte-784042c1.css              3.70 KiB / gzip: 1.04 KiB
.svelte-kit/output/client/_app/chunks/vendor-836cc791.js                                 14.11 KiB / gzip: 6.00 KiB
vite v2.6.14 building SSR bundle for production...
✓ 39 modules transformed.
.svelte-kit/output/server/app.js                          0.10 KiB
.svelte-kit/output/server/chunks/app-2634a22d.js          52.08 KiB
.svelte-kit/output/server/chunks/index.json-288c2c7b.js   0.42 KiB
.svelte-kit/output/server/chunks/_api-cf43c630.js         0.60 KiB
.svelte-kit/output/server/chunks/_uid_.json-095450d2.js   0.41 KiB
.svelte-kit/output/server/chunks/_id_-10adbe80.js         0.27 KiB
.svelte-kit/output/server/chunks/post-ffc560ba.js         0.27 KiB
.svelte-kit/output/server/chunks/__layout-69631abe.js     4.58 KiB
.svelte-kit/output/server/chunks/error-697bc48e.js        0.75 KiB
.svelte-kit/output/server/chunks/index-5e39d934.js        8.06 KiB
.svelte-kit/output/server/chunks/about-bf5177a7.js        1.32 KiB
.svelte-kit/output/server/chunks/index-dd838450.js        5.86 KiB

Run npm run preview to preview your production build locally.

> Using @sveltejs/adapter-static
> EISDIR: illegal operation on a directory, open 'build/api/post'
Error: EISDIR: illegal operation on a directory, open 'build/api/post'
    at Object.openSync (fs.js:476:3)                                                         
    at writeFileSync (fs.js:1467:35)

System Info

System:
    OS: Linux 5.11 Ubuntu 21.04 (Hirsute Hippo)
    CPU: (8) x64 Intel(R) Core(TM) i7-4710HQ CPU @ 2.50GHz
    Memory: 430.57 MB / 7.65 GB
    Container: Yes
    Shell: 5.1.4 - /bin/bash
  Binaries:
    Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
    npm: 7.13.0 - ~/.nvm/versions/node/v14.16.0/bin/npm
  Browsers:
    Brave Browser: 91.1.26.74
    Chromium: 95.0.4638.69
    Firefox: 94.0
  npmPackages:
    @sveltejs/adapter-static: ^1.0.0-next.21 => 1.0.0-next.21 
    @sveltejs/kit: next => 1.0.0-next.196 
    svelte: ^3.34.0 => 3.44.2

Severity

blocking all usage of SvelteKit

Additional Information

I believe, that the static adapter only checks for the existence of either file or folder, but does not distinguish between the two.

Will try to dig into during my free time if needed.

@lsabi
Copy link
Author

lsabi commented Nov 28, 2021

Okey, so that's what I found out after some digging the source code:

Both /api/post and /api/post/[id] are prerendered at build time, but if /api/post already exists and is a file, it is not possible to create a directory post and to populate it. Conversly, if /api/post/[id] exist, then it is not possible to create a file named /api/post, since a directory is a special kind of file under linux (which, I guess, is the most used OS for kit on the server side).

This leaves the open question of how to statically generate files for an endpoint.

A possible solution could be to add index.json during prerendering on every request to an endpoint, or let every prerendered endpoint end with .json in the file name.

@Rich-Harris
Copy link
Member

This is already called out in the docs https://kit.svelte.dev/docs/page-options#prerender-route-conflicts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants