Open pofider opened 8 years ago
Is there a workaround ?
My solution:
using express-static and http://localhost/ as template base path
e.g.
"<link
rel="stylesheet" type="text/css" href="http://localhost/public/style.css">"
Hi @pofider , Any news on this? Could you explain why relying on the incoming request url doesn't allow static-resources to work with jsreport.render() ?
I currently use as a workaround the combination of what you suggested and what @flottokarotto suggested:
Also, I tried to create "images", "stylesheets", and "javascripts" sub-directories inside staticResources directory. For some reason it doesn't work even in jsReport studio.
@vkelman This is actually the same way how this extension works. It exposes a directory through static express middleware which is then reached by phantomjs. It relies on incoming request because it needs to take from somewhere the address where the server sits. This is quite limiting. We should probably start extra http server, bind it to localhost:1234 and use it instead.
I think I understood. Yes, my code also relies on incoming request to get "the address where the server sits". When jsreport-core embedded into NodeJs app, at the moment a request is coming jsreport-static-resources is already initialized by jsReport.use(require('jsreport-static-resources')()) and it's too late to tell jsreport-static-resources what is URL for static is. Something like that, right?
@vkelman Hm, not exactly. The url is assembled at the time of the request. Take a look at the source, it is couple of lines. The problem is that if you start express http server, you actually don't know what is the hostname. If we want a solution working in phantomjs as well as public html, we need to rely on the incoming request to find out the hostname. The future solution will likely be different and rather inline scripts, styles and other resources than link them through http.
I am having issues with helper not generating correct URLs also.. In my case, I Ngnix in front of main app under "/" and jsreport under "/" Additionally, main app is using jsreport nodejs client to make a request and render report in an iframe....
So there are several situations in which the helper needs to generate correct URL.
For my case, since I am running main app and jsreport on same server, my workaround is two fold. A) I am using following location directive on Ngnix to make it work (this also has advantage of Ngnix serving files not reportjs/nodejs)
### serve files from jsreport-static-resources module via nginx
location /static-resources/ {
alias '../jsreport/data/staticResources/';
expires max;
#access_log off;
}
B) I am not using the helper {{staticResources}} in templates, but am including resources via direct path. Ex:
<link rel="stylesheet" href="/static-resources/components/bootstrap/dist/css/bootstrap-flex.min.css">
It was pain to come up with this workaround, and its not perfect (it 'pollutes' useable paths and may cause conflicts in another app needs it). I tried using locaiton of /reporting/static-resources/ but could not get it to work in all the cases....
Regards Z.
I've rethought the idea of this extension based on your inputs. Please take a look on the recent jsreport@1.1 release where I'm introducing a better way to handle this
tl;dr Lets embed external resources inline rather than referencing them with url
http://jsreport.net/blog/release-11-solves-external-files-referencing-problem http://jsreport.net/learn/assets
I will welcome every comment
Jan
1) In your blog you have animated gifs....Can you post some playground links to these examples? 2) I use bower to manage static assets (even with jsreport-static-resources)....Can I just link to those using this new extension 3) will this be a core module that replaces jsreport-static-resources ?
npm install jsreport@1.1.0 --production
and see yourself. There will be some blog posts for common usecases coming soon.I can see advantage of embedding assets for PhantomJS (as it can't process externally linked resources)... But for HTML, won't this have performance impact because 1) browser can load multiple external resources in parallel 2) browser can cache external resources that are not changing (e.g. external CSS and JS being used in template)
I was measuring performance impact for phantomjs and it was very small. There will be performance impact for browser html, that is true. However I think if you really care about it, it is better to use cdn. I have in plan to add an optional attribute like {#asset file.js @encoding=href} what you demand, but not sure when I implement it.
CDN is not an option in my case, as the app is installed internally, and most servers are prohibited from going out to Internet.
If the asset module will be replacing the static resources module, I think, it should be able to embed or link to an asset...
The extension relies on the incoming request url. It should be possible to switch to absolute file path and file protocol.