Worker request shows 200 and 500 on reload


#1

Every odd request I get a Error 1101 “Rendering error”… I tried a lot of different versions but it seems I cant get rid of it… this only happens when the client browser has caching on (default), via the web console with caching off or via holding shift and reloading it works every time. Somehow the second cached request breaks? I tried clearing the browsers cache and used different browsers including mobile and its the same outcome. Every second request = error.

Here is the full code to modify the content:

addEventListener('fetch', event => {
  event.respondWith(handle(event.request));
});

async function handle( request ) {
  const response = await fetch( request );
  if( response.headers.get( 'content-type' ).indexOf( 'text/html' ) !== -1 ) {
    try {
      const body = await response.text();
      return new Response(
        rewritebody( body, request ),
        response
      );
    }
    catch( e ) {
      return response;
    }
  }
  else {
    return response;
  }
}

function rewritebody( body, r ) {
  if( body.indexOf( '§REF:http' ) !== -1 ) {
    const match = body.match( /§REF:([^§]+)§/ );
    if( typeof match[1] !== 'undefined' ) {
      const url = match[1].replace( /\{r\}/, encodeURIComponent( r.url ) );
      body = body.replace( /<\!--.+?REF\:http[^>]+?-->/, '<!-- url:'+url+' -->' );
    }
  }
  return body;
}