Inject nocache.js into the main index.html

5 views
Skip to first unread message

Craig Mitchell

unread,
Feb 12, 2026, 5:54:23 PM (7 hours ago) Feb 12
to GWT Users
I tried to inject the non-dev nocache.js into my main index.html file.  Like this:

String gwtNoCacheJs = loadFileFromServlet("/dt/dt.nocache.js");

if (gwtNoCacheJs.contains("superdevmode")) {
  gwtNoCacheJs = "<script type='text/javascript' src='/dt/dt.nocache.js'></script>";
}
else {
  gwtNoCacheJs = "<script type='text/javascript'>\n" + gwtNoCacheJs + "\n</script>";
}

String html = loadFile("index.html").replace("XXX", gwtNoCacheJs);

However, it doesn't work, as nocache.js wants to load files from the same sub directory it's located in, and not the root directory.

Has anyone done this?  It probably isn't work the effort, as it'll only save one network call, but I was curious if it's possible.

Colin Alworth

unread,
Feb 12, 2026, 6:16:23 PM (6 hours ago) Feb 12
to GWT Users
I believe that https://github.com/gwtproject/gwt/blob/main/dev/core/src/com/google/gwt/core/ext/linker/impl/computeScriptBase.js is what you're going to want to read, or possibly replace on your classpath. Alternatively, subclass the CrossSiteIframeLinker to override getJsComputeScriptBase(LinkerContext) to provide your own file.

From that file:
/**
 * Determine our own script's URL by trying various things
 *
 * First - use the baseUrl meta tag if it exists
 * Second - look for a script tag with the src set to MODULE_NAME.nocache.js and
 *   if it's found, use it to determine the baseUrl
 * Third - if the page is not already loaded, try to use some document.write
 *   magic to install a temporary tag and use that to determine the baseUrl.
 *
 * This is included into the selection scripts
 * wherever COMPUTE_SCRIPT_BASE appears with underlines
 * on each side.
 */

The "Second" step is where you appear to be getting stuck - since there is no script tag with a src attr, the rest of the loading code doesn't know what to do. So, add a meta tag for baseUrl so the script knows where the other resources are loaded from.

Note that I haven't messed with this in years, and might have missed a point or two.

Craig Mitchell

unread,
Feb 12, 2026, 8:00:28 PM (4 hours ago) Feb 12
to GWT Users
Thanks Colin.  Dug around a little and figured out all I needed was:

<meta name="gwt:property" content="baseUrl=/dt/" />

("dt" is my GWT module short name)

Now everything works, including superdevmode.  Ie: No need for the gwtNoCacheJs.contains("superdevmode") check anymore.

I think my page does load a bit faster now (on subsequent times, not the first visit).  https://drift.team/ if anyone is curious what the end result root html source looks like.

Craig Mitchell

unread,
Feb 12, 2026, 10:33:56 PM (2 hours ago) Feb 12
to GWT Users
I should point out, this is only useful if you have a single page app.  If you load different html pages, don't do this, as you'll be reloading the nocache.js for each page.
Reply all
Reply to author
Forward
0 new messages