Igor Loskutov
Igor Loskutov

Reputation: 2344

How to get compiled meteor css bundle on server side

I have multiple scss stylesheets in client/ directory. I have one particular page that is being rendered server-side and being served statically without Meteor app (it is email unsubscription confirmation).

I want to load my main site css bundle on this page.

For this objective everything I need is just a text contents of this bundle or even better an absolute path. Problem is, Assets.getText() access only private/ directory.

However, Meteor knows about this bundle file path on server side as it serves it with index.html somehow.

Is there a way to do it by myself?

Upvotes: 0

Views: 647

Answers (2)

wildhart
wildhart

Reputation: 449

The accepted answer only works in development. In production the css filename is a hash, e.g. facc2661135083eeff000051c65e72e2ad910050.css instead of merged-stylesheets.css.

This works for me in development AND production:

let cssPath = __meteor_bootstrap__.serverDir.replace('/server','/web.browser');
cssPath += "/"+fs.readdirSync(cssPath).find(file => file.slice(-4)==".css");

FYI, I'm using this server-side to pre-render with above-the-fold css: https://forums.meteor.com/t/pre-rendered-landing-pages-with-critical-css/50626

Upvotes: 0

Alex028502
Alex028502

Reputation: 3824

If I understand the question correctly, from looking through https://github.com/meteor/meteor/blob/devel/packages/webapp/webapp_server.js, I can get mine like this:

path.join(
  path.dirname(
    path.join(
      __meteor_bootstrap__.serverDir,
      __meteor_bootstrap__.configJson.clientPaths['web.browser']
    )
  ),
  "merged-stylesheets.css"
)

on the server side. Change web.browser to web.cordova for the mobile version.

But if you want to include it on the static page, you can probably also just go like this:

<link rel="stylesheet" type="text/css" href="/merged-stylesheets.css">

depending on how you are serving the static page

Upvotes: 3

Related Questions