Some of the PyScript WASM files are large. For example, pyodide.asm.wasm
is ~9.5 MB. Correctly setting up your web server to serve these files takes some consideration. You should configure several items in your web server. This article covers Apache 2.4 running on Ubuntu 20.04. There are similar configurations for Nginx.
I plan to write another article showing how to configure a PyScript application to specify the PyScript origin using the <py-config>
directive.
Support the WASM Mime Type
If the Mime type application/wasm
is not set up, you might see errors like this:
1 |
Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'. |
For Ubuntu, edit the file /etc/apache2/mods-enabled/mime.conf
. Add the following line:
1 |
AddType application/wasm .wasm |
Enable WASM Compression
WASM files are compressible. Properly setting up compression for WASM files can reduce network bandwidth and download time.
For Ubuntu, edit the file /etc/apache2/mods-enabled/deflate.conf
. Add the following line:
1 |
AddOutputFilterByType DEFLATE application/wasm |
Precompress WASM Files
Apache tries to compress files on the fly at each request. This will consume CPU cycles. Precompress WASM files to .wasm.gz
and use content negotiation (Multiviews).
Multiviews is a per-directory option. Multiviews can be set with an Options directive within a <Directory>
, <Location>
or <Files>
section in apache2.conf.
Edit /etc/apache2/apache2.conf
. Add the following lines within one of the supported sections (usually <Directory>
):
1 2 3 4 |
Options Multiviews RemoveType .gz AddEncoding x-gzip .gz AddType application/wasm .wasm |
Summary
Hopefully, this article provided you with some new information about PyScript and how to deliver those files to your consumers.
More Information
- Other articles that I have written on Pyscript
- Apache 2.4 Content Negotiation
- Apache 2.4 Module mod_mime
- MDN: Properly configuring server MIME types
Photography Credit
I write free articles about technology. Recently, I learned about Pexels.com which provides free images. The image in this article is courtesy of Harvey Sapir at Pexels.
I design software for enterprise-class systems and data centers. My background is 30+ years in storage (SCSI, FC, iSCSI, disk arrays, imaging) virtualization. 20+ years in identity, security, and forensics.
For the past 14+ years, I have been working in the cloud (AWS, Azure, Google, Alibaba, IBM, Oracle) designing hybrid and multi-cloud software solutions. I am an MVP/GDE with several.
Leave a Reply