TheSharpieOne
TheSharpieOne

Reputation: 25726

Client-side caching with IISNode

I am running IISNode on IIS 8.5 and am not able to enable client side caching of static files. The files are served up without touching node using IISNode. When the files are served with IISNode they contain the Cache-Control: no-cache header.

If I were to host just node and bypass IIS and IISNode I get Cache-Control:public, max-age=604800 for the header.

Somewhere IIS or IISNode is setting the cache-control value. I cannot seem to change it in IIS, as when I do I get Cache-Control:no-cache,public,max-age=604800

How can I prevent no-cache from being added to the cache-control header?

Upvotes: 2

Views: 1307

Answers (1)

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26278

Try if any of these work:

1) Set the cache in IISNode: app.use(express.static(path.join(__dirname, 'public'), {maxAge: 86400000}));

2) Add new IIS rule to cache all the resposes from youriisnode.js

enter image description here

OR

3)

The best way to serve static content in iisnode is to configure the URL rewriting module such that the IIS static file handler handles requests for static content rather than node.js. Having IIS serve static content has a large performance benefit over serving these files using any node.js mechanisms due to kernel level optimizations around caching and just not having to break into JavaScript code.

Create web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>         
      <handlers>
           <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
     </handlers>
      <rewrite>
           <rules>

                <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
                     <match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$"/>
                </rule>

                <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">                    
                    <match url="^server.js\/debug[\/]?" />
                </rule>

                <rule name="StaticContent">
                     <action type="Rewrite" url="public{REQUEST_URI}"/>
                </rule>

                <rule name="DynamicContent">
                     <conditions>
                          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
                     </conditions>
                     <action type="Rewrite" url="server.js"/>
                </rule>

           </rules>
      </rewrite>
   </system.webServer>
 </configuration>

What this does is the following:

  • assumes server.js is the entry point to your node.js application that will receive HTTP requests for all URL paths except:

    1. requests for logs (/server.js.logs/0.txt, /server.js.logs/1.txt, etc.),

    2. debugging requests (/server.js/debug),

    3. requests for physical files that exist in the public subdirectory (e.g. request for /styles.css will be handled by the static file handler in IIS rather than your node.js application IFF that file exists at the \public\styles.css location).

Requests for all other URLs (e.g. /a/b/c?foo=12) will now be sent to the server.js application and will be handled following the logic implemented there. In case of an Express app, express routes will apply.

Original source: https://github.com/tjanczuk/iisnode/issues/160#issuecomment-5606547

Upvotes: 1

Related Questions