Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Serving files

From MoodleDocs

There are a lot of ways that files of various types can be served in Moodle and various API to help do it correctly. This page aims to link together all the various ways in one place with the pros and cons and best practice of each way.

Factors to consider

Security

Performance and caching

One of the driving

Ways of serving files

Javascript files

For all javascript files whether files you have written yourself or 3rd party bundle libraries see: Javascript Modules

CSS files

Themes overview#CSS

File API and pluginfile.php

File API#Serving files to users

Data format API

If you need to generate a structured file on demand such as a CSV or Excel it is generally best to use the Data formats API. The Data format API can also be used in conjunction with the pluginfile API.

Direct from Apache / nginx

This is only included for completeness and in general you should almost never serve files of any type directly from your web server. As a rule of thumb you should be able to comment out the native file serving directives in your nginx config and everything should still work because all files are served through php rather than directly. This may seem like it would be worse for performance in some cases, but it is actually better as Moodle is designed to sit behind a reverse proxy caching server or a CDN like Cloudflare, Cloudfront etc.