Make `src/routes/index.anything.js` a compile error
See original GitHub issueDescribe the bug
An endpoint with a filename of routes/index.json.ts
cannot be reached using a fetch
from a routes/index.svelte
On the vite process I see a:
SyntaxError: Unexpected token < in JSON at position 0
The server defaults to 500 and the fetch promise fails.
Using any other name for the endpoint (like routes/other.json.ts
) and pointing the fetch
call to this new endpoint works as expected.
This was introduced on a recent version of @sveltejs/kit
as this does not happen on previous versions this can be observed on : 1.0.0-next.260
Reproduction
- run the gernerator:
npm init svelte@next my-app
- before installing deps use
"@sveltejs/kit": "1.0.0-next.260"
- create a
routes/index.json.ts
with a handler:
import type { RequestHandler } from "@sveltejs/kit";
export const get: RequestHandler = async () => {
return {
status: 200,
body: {
user: "Dude",
},
};
};
- call the endpoint from
routes/index.svelte
<script context="module" lang="ts">
type Props = {
props: {
user: string;
};
};
export const load = ({ fetch }): Promise<Props> =>
fetch('/index.json') // does not work
// fetch('/other.json') // works
.then((res) => res.json())
.then(({ user }) => ({ props: { user } }))
.catch((res) => {
console.error(res)
return {
status: res.status,
error: new Error(`Could not load url`)
};
});
</script>
<...>
- Rename
routes/index.json.ts
to something different likeroutes/other.json.ts
and adjust thefetch
call and it works as expected
Logs
<can't provide the logs>
System Info
System:
OS: macOS 12.0.1
CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
Memory: 155.69 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.2/bin/yarn
npm: 8.1.2 - ~/.nvm/versions/node/v16.13.2/bin/npm
Browsers:
Edge: 97.0.1072.55
Safari: 15.1
npmPackages:
@sveltejs/adapter-auto: next => 1.0.0-next.17
@sveltejs/kit: 1.0.0-next.260 => 1.0.0-next.260
svelte: ^3.44.0 => 3.46.4
Severity
serious, but I can work around it
Additional Information
Reported on the help #kit-help channel too
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Can't resolve 'react-router-dom' - Stack Overflow
My api folder holds nothing but node_modules, my api file, route.js, config.js, index.js and also a package.json and package-lock.json. I have ...
Read more >What went wrong? Troubleshooting JavaScript - MDN Web Docs
Fixing syntax errors · Go to the tab that you've got number-game-errors. · This is a pretty easy error to track down, and...
Read more >Ahead-of-time (AOT) compilation - Angular
The AOT compiler detects and reports template binding errors during the build step before users can see them. Better security, AOT compiles HTML...
Read more >Parcel
$ parcel index.html Server running at http://localhost:1234 Build failed. @parcel/core: Cannot resolve 'ract' from './index.js' /dev/app/index.js: ...
Read more >Warning Options (Using the GNU Compiler Collection (GCC))
Make all warnings into errors. -Werror= Make the specified warning into an error. The specifier for a warning is appended; for example -Werror ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@babichjacob beat me to part of this by a few seconds. I’d probably argue that
src/routes/index.anything.js
orsrc/routes/index.anything.ts
should probably be a compile-time error.The only reference I see to
index.json.js
in the docs is inside a folder inroutes
rather than directly inroutes
.What path is
src/routes/index.json.js
expected to be accessed from, with the knowledge thatsrc/routes/index.svelte
is accessed fromwebsite.com/
?website.com.json
andwebsite.com/.json
are both definitely wrong, butwebsite.com/index.json
differs from the meaningindex
has elsewhere.