Showing posts with label static-resources. Show all posts
Showing posts with label static-resources. Show all posts

Tuesday, October 4, 2022

How to use static content / resource files in app root directory in grails

In Grails latest version like 4 and 5, it provides static assets management mechanism more standard way. We can use different files under the corresponding folders.
All the images will go under the images directory, js files go under the javascripts directory and CSS goes under the stylesheets directory. These files will be accessed by the URL path start with "/assets/".
Now, what if you don't want to use this structure or need to use the root path for some static file content.
For this, grails provide another option to manage static resources. So, what you can do is create the /resources/public directory under /src/main/ and use static resources there.
For example, if you had a file under /src/main/resources/public/images/example.jpg path then you can access that file using https://example.com/static/images/example.jpg.
This means, your file can be accessed with "/static/" URL by default. If you want to access the file in the app root directory, setup the following configuration under application.groovy

grails.resources.pattern = '/**'
Now, for the above example the file can be accessed with https://example.com/images/example.jpg URL and if the file is in /public/ directory that file can be accessed with root path https://example.com/file.js