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

Import from static folder #9

Closed
DanielRios549 opened this issue Nov 24, 2021 · 4 comments
Closed

Import from static folder #9

DanielRios549 opened this issue Nov 24, 2021 · 4 comments
Labels
question Further information is requested

Comments

@DanielRios549
Copy link

When import SVGs from static folder, either with relative path or using an alias, this error occurs:
files in the public directory are served at the root path. Instead of /static/icons/file.svg?import, use /icons/file.svg?import

The problem is that if I ommit the static folder, it says the SVG is not a valid SSR Component.

@poppa poppa added the bug Something isn't working label Nov 25, 2021
@poppa
Copy link
Owner

poppa commented Nov 25, 2021

Correct me if I'm wrong, but this is a warning and not an error, right?

From what I can tell this is a warning from Vite so this seems like something I can't fix in the plugin. I managed to find this deep inside Vite:

// check if public dir is inside root dir
const publicDir = normalizePath$4(server.config.publicDir);
const rootDir = normalizePath$4(server.config.root);
if (publicDir.startsWith(rootDir)) {
    const publicPath = `${publicDir.slice(rootDir.length)}/`;
    // warn explicit public paths
    if (url.startsWith(publicPath)) {
        logger.warn(source.yellow(`files in the public directory are served at the root path.\n` +
            `Instead of ${source.cyan(url)}, use ${source.cyan(url.replace(publicPath, '/'))}.`));
    }
}

My reading of this is that you either have to live with the warning, or simply move the SVG you'r importing as a component within your source directory.

Also, this warning only seems to occur when running svelte-kit dev.

Unless you provide some further info about this problem (Svelte version, adapter type, is it dev/prod mode, is it building correctly or not, etc...) I will close this issue in a couple of days.

Thanks for the report nonetheless.

@poppa poppa added question Further information is requested and removed bug Something isn't working labels Nov 25, 2021
@DanielRios549
Copy link
Author

Ok, it definitely seems to be a development warning only, when the project is built, it put the SVG inline in HTML and the warning does not accours.

I am using SvelteKit in next version (at least this is what it says in package.json), and the adapter-static to build. Using this adapter, I think there is no need of put the SVGs inside static folder.

@poppa
Copy link
Owner

poppa commented Nov 25, 2021

When you use this plugin the SVGs you import should rather reside within your "source" folder. Note that when used like this the SVGs are more or less compile-time resources rather than runtime resources, hence why you shouldn't put resources used by this plugin in the static folder.

@DanielRios549
Copy link
Author

This is what I was intended to do, I thought the SVG are put inline in runtime, not in build, so I put then in static folder in order to cache them with a service worker.

But as they are generated in build time, I move them to source folder and the warning goes away, but even using static folder, it builds normally, the warning is only when in development.

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

No branches or pull requests

2 participants