sveltejs / sapper

The next small thing in web development, powered by Svelte
https://sapper.svelte.dev
MIT License
6.99k stars 432 forks source link

Exporting a server route called index.js prevents routes further down the filesystem hierarchy from being exported #1170

Open antony opened 4 years ago

antony commented 4 years ago

Describe the bug A bit of a mouthful.

If I have:

src
src/routes
src/routes/somedir/index.js
src/routes/somedir/[slug].js

The final server route ([slug].js) will fail to export with an error ENOTDIR.

The reason being that the following directory structure has been exported:

__sapper__/export/somedir [FILE]

which should be a [DIR] so that the nested routes can be exported to it.

Logs

> Built in 15.2s
> Crawling http://localhost:3001/blog/
    440 kB   api/v1/posts
    380 kB   index.html
     15 kB   api/v1/posts/highest-mountains-in-england

internal/fs/utils.js:229
    throw err;
    ^

Error: ENOTDIR: not a directory, open '/home/ant/Projects/beyonk-blog/web/__sapper__/export/blog/api/v1/posts/highest-mountains-in-england'
    at Object.openSync (fs.js:456:3)
    at Object.writeFileSync (fs.js:1297:35)
    at save (/home/ant/Projects/beyonk-blog/web/node_modules/sapper/dist/export.js:1800:6)
    at ChildProcess.<anonymous> (/home/ant/Projects/beyonk-blog/web/node_modules/sapper/dist/export.js:1805:3)
    at ChildProcess.emit (events.js:305:20)
    at emit (internal/child_process.js:883:12)
    at processTicksAndRejections (internal/process/task_queues.js:85:21) {
  errno: -20,
  syscall: 'open',
  code: 'ENOTDIR',
  path: '/home/ant/Projects/beyonk-blog/web/__sapper__/export/blog/api/v1/posts/highest-mountains-in-england'
}

To Reproduce Create a directory structure as above. Having some machine issues right now so I can't create a repo, but it is inbound asap.

Expected behavior I'm not exactly sure. I think that the index route should probably end up in an index file inside somedir so that the rest of the routes can be exported.

Information about your Sapper Installation:

Severity I'm fairly sure I can work around it, but it's a problem for new users. I also know that it has caused a problem for the folk over at sanity.io, since their Sapper template doesn't actually result in a true static site, and you end up getting a couple of API requests per view which can rack up. I've raised https://github.com/sanity-io/sanity-template-sapper-blog/issues/42 which I am trying to fix.

Conduitry commented 4 years ago

This gotcha is already documented here. We can handle page routes by making them all be folders containing index.html files, but that doesn't work well with server routes, because we probably can't rely on static servers having the same index.* handling for other file types.

antony commented 4 years ago

Ah, yes I hadn't quite understood that the route conflicts section pertained to this specific problem.

I wonder if there's a better way to warn the user than just throwing an error.