Skip to content

Files from static folder not found in dev #1141

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

Closed
goowikns opened this issue Dec 4, 2017 · 14 comments
Closed

Files from static folder not found in dev #1141

goowikns opened this issue Dec 4, 2017 · 14 comments

Comments

@goowikns
Copy link

goowikns commented Dec 4, 2017

I seem to be having an issue where in dev mode my static files are not found.

To recap:

  • Vue-cli version: 2.9.1 (seems to include assetsPublicPath: '/', which seem to be an issue in previous vue versions)
  • files under static folder not found during npm run dev
  • within index.html I have following path: <script src="/static/scriptname.js"></script>

The built version does work!

@LinusBorg
Copy link
Contributor

LinusBorg commented Dec 4, 2017

work fine for me ... you will have to provide more information.

image

image

@goowikns
Copy link
Author

goowikns commented Dec 5, 2017

That works cause the logo (in a new vue projects) resides in the /src/assets folder and not in the /static folder (where I put my javascript files)

@LinusBorg
Copy link
Contributor

LinusBorg commented Dec 5, 2017

No, I moved it to the static folder. if it were in the assets folder and handled by webpack, it would also be under static/img, not static/

@goowikns
Copy link
Author

goowikns commented Dec 5, 2017

I don't understand why it doesn't seem to work.

As I said, I have 2 js files in my projects-root-static-folder.
I simply included them in my index.html using: <script src="/static/scriptname.js"></script>

Now when I run dev mode, the files don't seem to be 'virtually' copied to the static folder. All the rest from the src/assets folder is! Just not the static folder.

On building the project, it does work. Can it be there is a bug or something missing from the dev config?

@LinusBorg
Copy link
Contributor

Hard to say without seeing your actual setup. Your description can't replace that.

@goowikns
Copy link
Author

goowikns commented Dec 6, 2017

I created a fresh vue project and started testing.

It seems scripts and sounds aren't copied at all in dev mode. Pictures like the logo in your example are copied correctly.

@LinusBorg
Copy link
Contributor

Hm, that's weird. I'll have to check out if this is somethign webpack-dev-server does on purpose ... we haven't specified anything particular about what to serve, webpack-dev-server just does this on its own from the root directory.

@goowikns
Copy link
Author

goowikns commented Dec 7, 2017

I checked their documentation at https://webpack.js.org/configuration/dev-server/#devserver-publicpath-

It says

Make sure publicPath always starts and ends with a forward slash.

But even using the forward slashes doesn't seem to work.

@goowikns
Copy link
Author

Any update on this issue? Thank you 👍

@LinusBorg
Copy link
Contributor

thanks for the reminder.

I just did the following:

  • create a new project
  • add a file called test.js in the /static folder. it looks like this:
console.log('This was a successful test!')
  • add a script element to import this in index.html
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <title>test-static</title>
</head>
<body>
  <div id="app"></div>
  <script src="/static/test.js"></script>
  <!-- built files will be auto injected -->
</body>
</html>

....and it works:

bildschirmfoto 2017-12-13 um 10 26 17

So it works just like expected.

I will stop researching this unless you can provide a repository that demonstrates the problem with runnable code. And please don't share a whole project that you worked on - just the bare minimum to demo it.

Otherwise, I will close this issue since a reproduction of your problem is not possible.

@nkovacs
Copy link

nkovacs commented Dec 16, 2017

I'm not sure if I ran into the same problem. I changed assetsSubDirectory to an empty string and that breaks static file serving.

Before you switched to using webpack-dev-server instead of your custom dev server, you had this: https://github.com/nickspiel/webpack/blob/b4b8154a7dbcc469069a94b017dc2d56a429e8f2/template/build/dev-server.js#L57
This worked fine because it was serving ./static as whatever you decided to rename your assetsSubDirectory to.

Now it doesn't work any more because webpack serves all files in the working directory. The working directory is set to the project root when you start the dev server with npm run or yarn run, so your entire working directory is served by webpack-dev-server (see https://webpack.js.org/configuration/dev-server/#devserver-contentbase). This means that the default assetsSubDirectory only works because the source directory is also called static. As soon as you change assetsSubDirectory, it breaks.
However, under e2e tests it works because it uses the production config and CopyWebpackPlugin. Using CopyWebpackPlugin in the dev config too instead of relying on contentBase fixes this problem, at least with assetsSubDirectory set to an empty string.

@LinusBorg
Copy link
Contributor

LinusBorg commented Dec 16, 2017

Thanks for the analysis. I came to similar conclusions when researching #1176. That issue is probably more related to your problem.

This issue here, according to OP, has an assetPublicPath of '/' which should be fine.

I'm still researching horny invest tackle this with webpack-dev-server. If you have any ideas, please share them (in #1176)

@LinusBorg
Copy link
Contributor

I will close this since there's still no reproduction available And I can't see what's supposed to be wrong.

@goowikns Please open a new issue pointing to this one if you can create a repository that actually reproduces the issue

@juicechu
Copy link

@nkovacs it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants