django-webpack-loader icon indicating copy to clipboard operation
django-webpack-loader copied to clipboard

Inline bundle rendering

Open IlyaSemenov opened this issue 8 years ago • 9 comments
trafficstars

That would be great to allow rendering bundles inline rather than linking to them. This will speed up page loading for smaller bundles (e.g. having facebook/twitter sharing scripts combined into a single bundle).

For example, the following syntax:

{% render_bundle 'social' 'js' inline=True %}

would emit:

<script type="text/javascript">
// here goes the content of social.e9e35ae5.js
</script>

IlyaSemenov avatar Feb 08 '17 07:02 IlyaSemenov

This might also be useful for including standalone chunk manifests.

imelnych avatar Feb 20 '17 15:02 imelnych

Sounds reasonable but I'll need some time to think about how to implement it and how to handle all cases (bundles stored at remote location, caching, code extension points). If anyone wants to take a stab at this, please share your proposed solution here before starting the work.

owais avatar Feb 20 '17 16:02 owais

FYI, there is a similar project python-webpack-manifest where the maintainer has already merged my PR for inline manifests (https://github.com/markfinger/python-webpack-manifest/pull/5).

I like the framework-agnostic approach better. Architecture wise, there is no real need to be dependent on Django to pull manifest info.

IlyaSemenov avatar Feb 21 '17 06:02 IlyaSemenov

It seems to me that the solution to this will cover a lot of the same territory as #103 (subresource integrity support). Both will require addressing the content of assets, and caching details beyond the filename.

yourcelf avatar Feb 28 '17 23:02 yourcelf

I have some code in a project of mine which works quite well. The idea is to add an additional setting, BUNDLE_PATH, and a template tag which first tries to render the bundle inline, then falls back to including a reference to a remote bundle. This also works well when using webpack-dev-server, because then the files never exist on the hard disk, and because of that the fallback is always used.

(Of course we'd probably want to add a new argument to render_bundle, for example prefer_inline=False, instead of adding a new template tag.)

I added the code in a gist: https://gist.github.com/matthiask/a4c301f61021614b19cd14d4e8979774

@owais Any feedback? If you like the proposal I'll happily clean it up for integration into the official repo.

matthiask avatar Sep 11 '17 08:09 matthiask

@matthiask After referring to gist link, I found a reasonable solution to this issue. add static assets inline output features

ghost avatar Sep 14 '17 01:09 ghost

@liuliangsir STATICFILES_DIRS and WEBPACK_BUNDLE_DIR both are not guaranteed to be set. I'd much rather have an additional key in the WEBPACK_LOADER dicts than some non-obvious (to me) concatenation of other variables. Also; I'm not sure why you'd want to (ab)use attrs to pass the inline argument -- it overloads the meaning of attrs. Adding another argument to the template tag seems much cleaner to me.

matthiask avatar Sep 14 '17 06:09 matthiask

This requires not only support for reading files from local disk but also from remote sources as assets are often stored at remove locations in production. Anyone wants to take a shot at this, please don't forget to handle remote assets in a reliable way

owais avatar Nov 24 '17 05:11 owais

I published a good enough solution here: https://pypi.org/project/django-webpack-bundle/

It's not perfect and it especially does not support remove static assets but that's out of scope for me.

matthiask avatar Jun 15 '19 09:06 matthiask

Closing, but keeping https://github.com/django-webpack/django-webpack-loader/issues/234 open.

fjsj avatar Nov 29 '23 20:11 fjsj