-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Server asset path mismatch when running vite preview
#11020
Comments
Hm. This feels to me like something that shouldn't be our responsibility. IIRC, running For the production output from the Node adapter, I don't feel like the generated code should default to changing the CWD at all. It should just accept what the user called the built code with. If you have particular CWD needs, you can put something at the top-level in your hooks file. In any case, changes to these defaults (whether in dev/preview mode, or in Node adapter output) would be a breaking change for anyone who is already resolving anything relative to the CWD. |
Yes,
That's true, but I hope we'd at least change the default for preview mode to remove one of these steps. // +page.server.js
import path from 'node:path';
import fs from 'node:fs';
import text_file from './file.txt';
import { env } from '$env/dynamic/private';
let filepath;
if (!dev && env.NODE) {
// `node build`
filepath = path.join(process.cwd(), 'build/server', text_file);
}
else if (!dev) {
// `npm run preview`
filepath = path.join(process.cwd(), '.svelte-kit/output/server', text_file);
}
else {
// `npm run dev`
filepath = path.join(process.cwd(), text_file);
}
export async function load() {
const text = fs.readFileSync(filepath, { encoding: 'utf-8' });
return { text };
} |
If at all we should only change this for |
Sounds good. Should we at least move the server assets (or all server files) to the same directory as the recommended cwd the node process runs at? Currently, the assets live in a subfolder named “server/…”, so a prod build with adapter-node would need to prepend every asset path with “server/“ |
So that means that |
That’s right. I only came across this when someone asked for a demo of a file upload to a node server with sveltekit. I wonder if people have just been working around by changing the path, starting the node process in the server folder, or moving the files with a postbuild script. |
Rich had the nice idea of making this a function |
Would the function return a consistent root directory, or would it actually chdir to that directory? Either way, I think |
I'm not sure I fully understand. Would that solve both the |
It would need to be written in such a way that it would. I guess it actually would be good to not touch |
I misunderstood the problem when I proposed this earlier. I thought the issue was that path.join(cwd(), 'file-in-my-project-directory.text'); ...but that's not the case – // +page.server.js
import path from 'node:path';
import fs from 'node:fs';
+import { serverAssets } from '$app/paths';
import text_file from './file.txt';
export async function load() {
// we can get the absolute path with `process.cwd()`
- const filepath = path.join(process.cwd(), text_file);
+ const filepath = path.join(serverAssets, text_file);
const text = fs.readFileSync(filepath, { encoding: 'utf-8' });
return { text };
} Seems simple enough to implement. The challenge is making it visible — how do we force people to use |
The only options I can think of right now are to purposefully |
I was thinking more along the lines of 'convince Vite to stick a prefix in front of imported asset URLs', so that |
I'm inclined to remove this from the 2.0 milestone, since I don't see how we can fix it without adding something like |
vite preview
vite preview
vite preview
Describe the bug
Typically, we're suppose to access a server asset by joining
process.cwd()
and the path of the asset imported and handled by Vite.However, this doesn't work when running
vite preview
ornode build
with the Node adapter.This can be fixed by changing the current directory when
vite preview
is run. Hence, the filepath would correctly return/.svelte-kit/output/server/_app/...
instead of the project root/_app/...
which doesn't exist.https://github.com/sveltejs/kit/blob/master/packages/kit/src/exports/vite/preview/index.js
A similar thing occurs when we use
adapter-node
. The path returned byprocess.cwd()
keeps changing depending on from which directory we start the server. This could be considered the intended behaviour, but it also leads to unwanted behaviour (such as being unable to correctly resolve the filepath as shown in the example above).Reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-y1duvx?file=src%2Froutes%2F%2Bpage.server.js
pnpm dev
, this has no issue.pnpm build
.pnpm preview
. The page cannot render because the filepath is incorrect in the load function.Logs
System Info
Severity
serious, but I can work around it
The text was updated successfully, but these errors were encountered: