Search code examples
htmlbrowser-cachespark-framework

How do i ensure my webpages are being cached with spark-framework


My web application is powered by Java Spark framework The user makes a httppost request, we process the request and generate the page, clearly the page we send back is not cached and we would not want it to be.

But in its header it refers to various css, js and img files, these do not change. How do I ensure that these pages are cached, how do I even check if they are being cached, I dont currently know if they are or not.

<head>
  <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="style/googlefonts.css" rel="stylesheet" type="text/css">
  <link href="style/songkong.css" rel="stylesheet" type="text/css">
  <link href="style/jquery/ui/css/smoothness/jquery-ui-1.9.2.custom.css" rel="stylesheet" type="text/css">
  <link href="style/jstree/themes/style.css" rel="stylesheet" type="text/css">
  <script src="style/songkong.js" type="text/javascript"></script>
  <script src="style/jquery/jquery-1.9.1.js" type="text/javascript"></script>
  <script src="style/jquery/ui/js/jquery-ui-1.9.2.custom.js" type="text/javascript"></script>
  <script src="style/jstree/jstree.js" type="text/javascript"></script>
  <link rel="shortcut icon" href="style/songkong32.png" type="text/png">
  <link rel="icon" href="style/songkong32.png" type="text/png">
</head>

Solution

  • When using Java Spark Framework to cache static files referred to in html pages you need to set the expireTime (in seconds) on the staticFiles class

    e.g

    Spark.staticFiles.expireTime(600)
    

    caches for 10 minutes

    By default there is no caching

    Since non static files are generally being dynamically created with Java Spark I am not sure if there is any need to cache anything else.