Configure hosting behavior
Configure hosting behavior
With Firebase Hosting, you can configure customized hosting behavior, including custom error pages, redirects,rewrites, and headers. You can also specify which files to deploy from your project directory to your Firebase project.
Find your
firebase.json
file in the root of your project directory. Firebase automatically creates your firebase.json
file when you run the firebase init
command.
You can find a full
firebase.json
configuration example (covering only Firebase Hosting) at the bottom of this page. Note that a firebase.json
file can also contain configurations for other Firebase services.Priority order of Hosting responses
The different Firebase Hosting configuration options described on this page can sometimes overlap. If there is a conflict, Hosting determines its response using the following priority order:
- Reserved namespaces that begin with a
/__/*
path segment - Configured redirects
- Exact-match static content
- Configured rewrites
- Custom 404 page
- Default 404 page
Specify files to deploy
The default attributes —
public
and ignore
— included in the default firebase.json
file define which files in your project directory should be deployed to your Firebase project.
The default
hosting
configuration in a firebase.json
file looks like this:public
Required
The
The
public
attribute specifies which directory to deploy to Firebase Hosting. The default value is a directory named public
, but you can specify any directory's path, as long as it exists in your project directory.
The following is the default specified name of the directory to deploy:
You can change the default value to the directory that you want to deploy:
ignore
Optional
The
The
ignore
attribute specifies the files to ignore on deploy. It can take glob pattern the same way that Githandles .gitignore
.
The following are the default values for the files to ignore:
Customize a 404/Not Found page
Optional
You can serve a custom
You can serve a custom
404 Not Found
error when a user tries to access a page that doesn't exist.
Create a new file in your project's
public
directory, name it 404.html
, then add your custom 404 Not Found
content to the file.
Firebase Hosting will display the content of this custom
404.html
page if a browser triggers a 404 Not Found
error on your domain or subdomain.Configure redirects
Optional
Use a URL redirect to prevent broken links if you've moved a page or to shorten URLs. For example, you could redirect a browser from
Use a URL redirect to prevent broken links if you've moved a page or to shorten URLs. For example, you could redirect a browser from
example.com/team
to example.com/about.html
.
Specify URL redirects by including a
redirects
attribute within hosting
in your firebase.json
file. For example:
The
redirects
attribute contains an array of redirect rules, where each rule must include:- A
destination
, which is a static URL that can be a relative or an absolute path - A
type
specifying the HTTP response code
Firebase Hosting compares the
source
value against all URL paths at the start of every request (before the browser determines whether a file or folder exists at that path). If a match is found, then the Firebase Hosting origin server sends an HTTP redirect response telling the browser to make a new request at the destination
URL.
Finally, the
type
value specifies the specific HTTP response code served and can either be 301
for 'Moved Permanently' or 302
for 'Found' (Temporary Redirect).Capture URL segments for redirects
Optional
Sometimes, you might need to capture specific segments of a redirect
Sometimes, you might need to capture specific segments of a redirect
source
URL, then re-use these segments in the redirect destination
URL.
You can capture these segments by including a
:
prefix to identify the segment. If you also need to capture the remaining URL path after the segment, include a *
immediately after the segment. For example:Configure rewrites
Optional
Use a rewrite to show the same content for multiple URLs. Rewrites are particularly useful with pattern matching, as you can accept any URL that matches the pattern and let the client-side code decide what to display.
Use a rewrite to show the same content for multiple URLs. Rewrites are particularly useful with pattern matching, as you can accept any URL that matches the pattern and let the client-side code decide what to display.
You can also use rewrites to support apps that use HTML5 pushState for navigation. When a browser attempts to open a specified
source
URL, it will be given the contents of the file at the destination
URL.
Specify URL rewrites by including a
rewrites
attribute within hosting
in your firebase.json
file. For example:
The
rewrites
attribute contains an array of rewrite rules, where each rule must include:- A
destination
, which is a local file that must exist
Firebase Hosting only applies a rewrite rule if a file or directory does not exist at the specified
source
. When a rule is triggered, the browser returns the actual content of the specified destination
file instead of an HTTP redirect.Direct requests to a function
You can use
rewrites
to serve a function from a Firebase Hosting URL. The following example is an excerpt from serving dynamic content using Cloud Functions.
For example, to direct all requests from the page
/bigben
on your Hosting site to execute the bigben
function:
After adding this rewrite rule and deploying to Firebase (using
firebase deploy
), your function is reachable via the following URLs:- Your Firebase subdomains:
projectID.web.app/bigben
andprojectID.firebaseapp.com/bigben
Direct requests to a Cloud Run container
You can use
rewrites
to access a Cloud Run container from a Firebase Hosting URL. The following example is an excerpt from serving dynamic content using Cloud Run.
For example, to direct all requests from the page
/helloworld
on your Hosting site to trigger the startup and running of a helloworld
container instance:
After adding this rewrite rule and deploying to Firebase (using
firebase deploy
), your container image is reachable via the following URLs:- Your Firebase subdomains:
projectID.web.app/helloworld
andprojectID.firebaseapp.com/helloworld
Create custom domain Dynamic Links
You can use
rewrites
to create custom domain Dynamic Links. Visit the Dynamic Links documentation for detailed information about setting up a custom domain for Dynamic Links.
For example, you can:
- Use your domain only for Dynamic Links:
- Specify path prefixes that you want to use for Dynamic Links:
Configuring Dynamic Links in your
firebase.json
file requires:- An
appAssociation
attribute set toAUTO
.- The default for
appAssociation
isAUTO
if you don't include the attribute in your configuration. - With this attribute set to
AUTO
, Hosting dynamically generatesassetlinks.json
andapple-app-site-association
files when they are requested.
- The default for
- A
rewrites
attribute for Dynamic Links containing an array of rewrite rules, where each rule must include:- A
source
specifying a path that you want to use for Dynamic Links- Unlike rules that rewrite paths to URLs, Dynamic Link rewrite rules can't contain regular expressions.
- A
dynamicLinks
attribute set totrue
Configure headers
Optional
Headers allow the client and the server to pass additional information along with a request or a response. Some sets of headers can affect how the browser handles the page and its content, including access control, authentication, caching, and encoding.
Headers allow the client and the server to pass additional information along with a request or a response. Some sets of headers can affect how the browser handles the page and its content, including access control, authentication, caching, and encoding.
Specify custom, file-specific response headers by including a
headers
attribute within hosting
in your firebase.json
file. For example:
The
headers
attribute contains an array of definitions, where each definition must include:- A
source
value that Hosting matches against the original request path, regardless of any rewrite rules. - An array of (sub-)
headers
that Hosting applies to the request path; each sub-header array must include aspecifiedkey
and avalue
.
You can learn more about
Cache-Control
in the Hosting section that describes serving dynamic content and hosting microservices.
Control .html
extensions
Optional
The
The
cleanUrls
attribute allows you to control whether or not URLs should include the .html
extension.
When
true
, Hosting automatically drops the .html
extension from uploaded file URLs. If an .html
extension is added in the request, Hosting performs a 301
redirect to the same path but eliminates the .html
extension.
Specify the inclusion of
.html
extensions by including a cleanUrls
attribute within hosting
in your firebase.json
file. For example:Control trailing slashes
Optional
The
The
trailingSlash
attribute allows you to control whether or not URLs should include trailing slashes.- When
true
, Hosting redirects URLs to add a trailing slash. - When
false
, Hosting redirects URLs to remove a trailing slash. - When unspecified, Hosting only uses trailing slashes for directory index files (for example,
about/index.html
).
Specify the inclusion of trailing slashes by including a
trailingSlash
attribute within hosting
in your firebase.json
file. For example:Glob pattern matching
Firebase Hosting configuration options make extensive use of the glob pattern matching notation with extglob, similar to how Git handles
gitignore
rules and Bower handles ignore
rules. This wiki pageis a more detailed reference, but the following are explanations of examples used on this page:**
— Matches any file or folder in an arbitrary sub-directory**/.*
— Matches any file beginning with.
(usually hidden files, like in the.git
folder) in an arbitrary sub-directory**/node_modules/**
— Matches any file or folder in an arbitrary sub-directory of anode_modules
folder, which can itself be in an arbitrary sub-directory of thepublic
directory**/*.@(jpg|jpeg|gif|png)
— Matches any file in an arbitrary sub-directory that ends with exactly one of the following:.jpg
,.jpeg
,.gif
, or.png
Full Hosting configuration example
The following is a full
firebase.json
configuration example for Firebase Hosting. Note that a firebase.json
file can also contain configurations for other Firebase services.
Comments
Post a Comment