Serving Yew App with a self-made server

So I've followed the Yew sample app tutorial, served the files using Miniserve as the tutorial suggests, and it worked great.

But since I eventually want to make an app that communicates with the server using WebSockets, I want to serve it using my own server.

I tried making one, following the Single Threaded Web Server chapter of the Rust Book

use std::{fs, thread};
use std::io::prelude::*;
use std::net::TcpListener;
use std::net::TcpStream;

fn main() {
    let server = TcpListener::bind("").unwrap();
    for stream in server.incoming() {

        let stream = stream.unwrap();

        thread::spawn(|| {

fn handle_connection(mut stream: TcpStream) {
    let mut buffer = [0; 1024]; buffer).unwrap();

    let contents = fs::read_to_string("static/index.html").unwrap();

    let response = format!(
        "HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n{}",


And it worked great, but when I tried serving the index.html file from the Yew example, I received an error in the browser saying:
Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec. from wasm.js: 1.

I tried looking around but couldn't find out what causes it or how to fix it.

Thanks in advance for the help!

When serving javascript files, make sure you add the "Content-Type: application/javascript" header.

Can you please add an example of how to apply the header? I tried adding it to the request but I just couldn't the valid way to do that

The same way as you're already adding Content-Length header. The only problem here is that this new header must be attached only to certain files, so the response formatting will be different depending on the requested path.
Note also that you're currently not serving JavaScript and WASM at all - your server responds with static/index.html on any request. You must read the request (to the buffer, as you're doing now) and get the requested file name from it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.