question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make `src/routes/index.anything.js` a compile error

See original GitHub issue

Describe 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 like routes/other.json.ts and adjust the fetch 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
Conduitrycommented, Feb 6, 2022

@babichjacob beat me to part of this by a few seconds. I’d probably argue that src/routes/index.anything.js or src/routes/index.anything.ts should probably be a compile-time error.

1reaction
babichjacobcommented, Feb 6, 2022

The only reference I see to index.json.js in the docs is inside a folder in routes rather than directly in routes.

What path is src/routes/index.json.js expected to be accessed from, with the knowledge that src/routes/index.svelte is accessed from website.com/? website.com.json and website.com/.json are both definitely wrong, but website.com/index.json differs from the meaning index has elsewhere.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found