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

confusion about nested file hierarchy styles #59

Open
ariesshrimp opened this issue Sep 11, 2016 · 2 comments
Open

confusion about nested file hierarchy styles #59

ariesshrimp opened this issue Sep 11, 2016 · 2 comments

Comments

@ariesshrimp
Copy link

ariesshrimp commented Sep 11, 2016

So this plugin is awesome, but I've read a bunch of uses and this part still doesn't make sense in the existing documentation. The extract-text-plugin will only generate one css file. So if you have a nested directory structure in your generated output, you need to dynamically point the <link> tags to different locations. For example:

public/
    | index.html             <- This file needs <link href="styles.css"/>
    | styles.css             <- extract-text-plugin generates this file
    | about/
       | index.html          <- This file needs <link href="../styles.css"/>
    | posts/
       | some-post-title/
           | index.html      <- This file needs <link href="../../styles.css"/>

You can circumvent the problem during development using webpack-dev-server or browser-sync or something that fools around with the path while serving. But I don't see how you can deploy code this way.

For example: https://css-tricks.com/css-modules-part-3-react/ has a working example using dev-server.

http://jxnblk.com/writing/posts/static-site-generation-with-react-and-webpack/ just uses dangerouslySetInnerHtml to hardcode a <style> tag into his root component. That seems pretty suboptimal.

Is there a way using the webpackStats object in your base template to dynamically generate the needed <link> tags down the hierarchy? I haven't seen any use-cases that circumvent this yet, is it a possible feature, or just a tricky implementation detail?

Thanks!

@ariesshrimp
Copy link
Author

ariesshrimp commented Sep 12, 2016

So right now my solution is to pass locals from the entry point to my template function and do a hacky check on the path:

// template.js
export default props => {
  const distanceToRoot = props.locals.path !== '/'
    ? props.locals.path.split('/').slice(1).map(level => '../').join('')
    : '/'
return <html lang="en">
    <head>
      <link href={ `${distanceToRoot}styles.css` } rel="stylesheet"/>
    </head>
    ...

Not sure if that's the best strategy, or if there's a better built-in way of handling this, but this might be helpful to others. It would be nice for this to be handled by default somehow.

@vitalyq
Copy link

vitalyq commented Sep 12, 2016

@joefraley the simplest solution is to use absolute paths, namely <link rel="stylesheet" href="/style.css">, that is what react-router-tutorial suggests.

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

No branches or pull requests

2 participants