Working with assets (JS, CSS, etc.) #146
Replies: 2 comments 5 replies
-
In many real-world use cases, you may want to run X in tandem with an HTTP server or reverse proxy. It is quite common to configure this server to handle access to any static assets and only forward requests to dynamic content to the actual application server. See https://framework-x.org/docs/best-practices/deployment/ for possible configuration options, depending on which web server you use. That said, you can also handle static assets with X. I'm still working on the docs as handling filesystem access is inherently blocking and requires some special care in long running applications. In either case, you can always return a response with any appropriate $app->get('/asset/main.1270.js', function () {
return new Response(
Response::STATUS_OK,
['Content-Type' => 'application/javascript'],
'alert("works");'
);
}); I hope this helps 👍 |
Beta Was this translation helpful? Give feedback.
-
In the long run I'd suggest adding a feature to serve static folders containing app assets on endpoints with configurable in-memory caching. On some use cases an infinite cache would certainly be enough as the whole app gets deployed anyways and for all others one could implement something like php-watcher to periodically check for changes and invalidate the cache for that file. For all the vendor stuff, large images etc. it certainly is recommended to use a cdn, but for app specific files I think this would be a nice addition. |
Beta Was this translation helpful? Give feedback.
-
Currently I'm trying to load a javascript asset from a URL(i.e.
asset/main.1270.js
), however it doesn't seem to work because response of the request is below:As you can see the content-type is
text/html
which is invalid. This should beapplication/javascript
in order for it to work in the browser.I was wondering if anybody has faced same problem and know how to fix it. Or is there a standard way of doing this?
Beta Was this translation helpful? Give feedback.
All reactions