GoogleChromeLabs / carlo

Web rendering surface for Node applications
Apache License 2.0
9.32k stars 309 forks source link

How to handle the `single page web application`? #140

Open zhengxiaoyao0716 opened 5 years ago

zhengxiaoyao0716 commented 5 years ago

Such as in react, we often pack all the routes into one html bundle. I expected to handle all the request route and redirected them to the index.html, is there some way to do it elegant?

As for now, I used those code to do it, but it not perfect way, for example, request for JSON files maybe also redirected unexpectly:

app.serveFolder(path.join(buildDir, 'static'), 'static');
app.serveHandler(async request => {
    if (request.resourceType() !== 'Document') {
      request.continue();
      return;
    }
    const headers = { 'content-type': 'text/html' };
    const body = await fsReadFile(path.join(buildDir, 'index.html'));
    request.fulfill({ headers, body });
  });