Day 5 of 5
⏱ ~60 minutes
Firebase in 5 Days — Day 5

Hosting and Analytics

Deploy to Firebase Hosting with a CDN, set up rewrites for SPAs, and add Firebase Analytics.

Firebase Hosting

firebase.json
{
  "hosting": {
    "public": "dist",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ],
    "headers": [
      {
        "source": "**/*.@(js|css)",
        "headers": [{"key": "Cache-Control", "value": "max-age=31536000"}]
      }
    ]
  }
}
Deploy
npm run build
firebase deploy --only hosting
# Live at your-app.web.app
Analytics
import { getAnalytics, logEvent } from 'firebase/analytics';
const analytics = getAnalytics(app);

// Log custom events
logEvent(analytics, 'post_created', {
  post_id: postId,
  category: postCategory
});

logEvent(analytics, 'button_click', {
  button_name: 'subscribe'
});

// Built-in: page_view, session_start, first_open tracked automatically
💡
Firebase Hosting serves your app from Google's CDN globally. With the SPA rewrite rule (**/index.html), client-side routing works for all URLs. Without it, refreshing on /about returns a 404.
📝 Day 5 Exercise
Deploy Your Firebase App
  1. R
  2. u
  3. n
  4. <
  5. c
  6. o
  7. d
  8. e
  9. >
  10. n
  11. p
  12. m
  13. r
  14. u
  15. n
  16. b
  17. u
  18. i
  19. l
  20. d
  21. <
  22. /
  23. c
  24. o
  25. d
  26. e
  27. >
  28. .
  29. D
  30. e
  31. p
  32. l
  33. o
  34. y
  35. t
  36. o
  37. F
  38. i
  39. r
  40. e
  41. b
  42. a
  43. s
  44. e
  45. H
  46. o
  47. s
  48. t
  49. i
  50. n
  51. g
  52. .
  53. A
  54. d
  55. d
  56. t
  57. h
  58. e
  59. S
  60. P
  61. A
  62. r
  63. e
  64. w
  65. r
  66. i
  67. t
  68. e
  69. r
  70. u
  71. l
  72. e
  73. .
  74. V
  75. e
  76. r
  77. i
  78. f
  79. y
  80. c
  81. l
  82. i
  83. e
  84. n
  85. t
  86. -
  87. s
  88. i
  89. d
  90. e
  91. r
  92. o
  93. u
  94. t
  95. i
  96. n
  97. g
  98. w
  99. o
  100. r
  101. k
  102. s
  103. b
  104. y
  105. n
  106. a
  107. v
  108. i
  109. g
  110. a
  111. t
  112. i
  113. n
  114. g
  115. t
  116. o
  117. a
  118. r
  119. o
  120. u
  121. t
  122. e
  123. a
  124. n
  125. d
  126. r
  127. e
  128. f
  129. r
  130. e
  131. s
  132. h
  133. i
  134. n
  135. g
  136. .
  137. A
  138. d
  139. d
  140. o
  141. n
  142. e
  143. c
  144. u
  145. s
  146. t
  147. o
  148. m
  149. a
  150. n
  151. a
  152. l
  153. y
  154. t
  155. i
  156. c
  157. s
  158. e
  159. v
  160. e
  161. n
  162. t
  163. .

Day 5 Summary

  • Firebase Hosting = global CDN, HTTPS by default, custom domains.
  • The **/index.html rewrite is required for SPAs (React Router, Vue Router).
  • Cache aggressively: JS/CSS files with hashed names can be cached for a year.
  • Firebase Analytics is free and works automatically for page views. Add custom events for key actions.
Finished this lesson?