|
| 1 | +== Static Files |
| 2 | + |
| 3 | +Static files are available via javadoc:Router[assets, java.lang.String] route. The `assets` route |
| 4 | +supports classpath and file-system resources. |
| 5 | + |
| 6 | +.Classpath resources: |
| 7 | +[source, java, role="primary"] |
| 8 | +---- |
| 9 | +{ |
| 10 | + assets("/static/*"); <1> |
| 11 | +} |
| 12 | +---- |
| 13 | + |
| 14 | +.Kotlin |
| 15 | +[source, kotlin, role="secondary"] |
| 16 | +---- |
| 17 | +{ |
| 18 | + assets("/static/*") <1> |
| 19 | +} |
| 20 | +---- |
| 21 | + |
| 22 | +<1> Map all the incoming request starting with `/static/` to the root of classpath |
| 23 | + |
| 24 | +- GET `/static/index.html` => `/index.html` |
| 25 | +- GET `/static/js/file.js` => `/js/file.js` |
| 26 | +- GET `/static/css/styles.css` => `/css/styles.css` |
| 27 | +
|
| 28 | +.File system resources: |
| 29 | +[source, java, role="primary"] |
| 30 | +---- |
| 31 | +{ |
| 32 | + assets("/static/*", Paths.get("static")); <1> |
| 33 | +} |
| 34 | +---- |
| 35 | + |
| 36 | +.Kotlin |
| 37 | +[source, kotlin, role="secondary"] |
| 38 | +---- |
| 39 | +{ |
| 40 | + assets("/static/*", Paths.get("www")) <1> |
| 41 | +} |
| 42 | +---- |
| 43 | + |
| 44 | +<1> Map all the incoming request starting with `/static/` to a file system directory `www` |
| 45 | + |
| 46 | +- GET `/static/index.html` => `www/index.html` |
| 47 | +- GET `/static/js/file.js` => `www/js/file.js` |
| 48 | +- GET `/static/css/styles.css` => `www/css/styles.css` |
| 49 | +
|
| 50 | +Individual file mapping is supported too: |
| 51 | + |
| 52 | +.Classpath: |
| 53 | +[source, role="primary"] |
| 54 | +---- |
| 55 | +{ |
| 56 | + assets("/myfile.js", "/static/myfile.js"); |
| 57 | +} |
| 58 | +---- |
| 59 | + |
| 60 | +.File system |
| 61 | +[source, role="secondary"] |
| 62 | +---- |
| 63 | +{ |
| 64 | + Path basedir = ...; |
| 65 | + assets("/myfile.js", basedire.resolve("/myfile.js")); |
| 66 | +} |
| 67 | +---- |
| 68 | + |
| 69 | +=== Static Site |
| 70 | + |
| 71 | +The `assets` route works for static sites too. Just need to use a special path mapping: |
| 72 | + |
| 73 | +.Classpath resources: |
| 74 | +[source, java, role="primary"] |
| 75 | +---- |
| 76 | +{ |
| 77 | + Path docs = Paths.get("docs"); <1> |
| 78 | + assets("/docs/?*"); <2> |
| 79 | +} |
| 80 | +---- |
| 81 | + |
| 82 | +.Kotlin |
| 83 | +[source, kotlin, role="secondary"] |
| 84 | +---- |
| 85 | +{ |
| 86 | + val docs = Paths.get("docs") <1> |
| 87 | + assets("/docs/?*", docs) <2> |
| 88 | +} |
| 89 | +---- |
| 90 | + |
| 91 | +<1> Serve from `docs` directory |
| 92 | +<2> Use the `/?*` mapping |
| 93 | + |
| 94 | +The key difference is the `/?*` mapping. This mapping add support for base root mapping: |
| 95 | + |
| 96 | +- GET `/docs` => `/docs/index.html` |
| 97 | +- GET `/docs/about.html` => `/docs/about.html` |
| 98 | +- GET `/docs/note` => `/docs/index.html` |
| 99 | + |
| 100 | +=== Options |
| 101 | + |
| 102 | +The javadoc:AssetHandler[] automatically handle `E-Tag` and `Las-Modified` headers. You can turn |
| 103 | +control these headers programmatically: |
| 104 | + |
| 105 | +.Asset handler options: |
| 106 | +[source, java, role="primary"] |
| 107 | +---- |
| 108 | +{ |
| 109 | + AssetSource www = AssetSource.create(Paths.get("www")); |
| 110 | + assets("/static/*", new AssetHandler(www) |
| 111 | + .setLastModified(false) |
| 112 | + .setEtag(false) |
| 113 | + ); |
| 114 | +} |
| 115 | +---- |
| 116 | + |
| 117 | +.Kotlin |
| 118 | +[source, kotlin, role="secondary"] |
| 119 | +---- |
| 120 | +{ |
| 121 | + val www = AssetSource.create(Paths.get("www")) |
| 122 | + assets("/static/*", AssetHandler(www) |
| 123 | + .setLastModified(false) |
| 124 | + .setEtag(false) |
| 125 | + ); |
| 126 | +} |
| 127 | +---- |
| 128 | + |
| 129 | +The `maxAge` option set a `Cache-Control` header: |
| 130 | + |
| 131 | +.Cache control: |
| 132 | +[source, java, role="primary"] |
| 133 | +---- |
| 134 | +{ |
| 135 | + AssetSource www = AssetSource.create(Paths.get("www")); |
| 136 | + assets("/static/*", new AssetHandler(www) |
| 137 | + .setMaxAge(Duration.ofDays(365)) |
| 138 | + ); |
| 139 | +} |
| 140 | +---- |
| 141 | + |
| 142 | +.Kotlin |
| 143 | +[source, kotlin, role="secondary"] |
| 144 | +---- |
| 145 | +{ |
| 146 | + val www = AssetSource.create(Paths.get("www")) |
| 147 | + assets("/static/*", AssetHandler(www) |
| 148 | + .setMaxAge(Duration.ofDays(365)) |
| 149 | + ); |
| 150 | +} |
| 151 | +---- |
0 commit comments