@@ -5,6 +5,9 @@ const errors = require('@tryghost/errors');
55const config = require ( '../../../shared/config' ) ;
66const urlUtils = require ( '../../../shared/url-utils' ) ;
77const tpl = require ( '@tryghost/tpl' ) ;
8+ const { cardAssets} = require ( '../../services/assets-minification' ) ;
9+ const themeEngine = require ( '../../services/theme-engine' ) ;
10+ const settingsCache = require ( '../../../shared/settings-cache' ) ;
811
912const messages = {
1013 imageNotFound : 'Image not found' ,
@@ -99,7 +102,6 @@ function createPublicFileMiddleware(location, file, mime, maxAge, options = {})
99102 } ;
100103}
101104
102- // ### servePublicFile Middleware
103105// Handles requests to robots.txt and favicon.ico (and caches them)
104106function servePublicFile ( location , file , type , maxAge , options = { } ) {
105107 const publicFileMiddleware = createPublicFileMiddleware ( location , file , type , maxAge , options ) ;
@@ -113,6 +115,56 @@ function servePublicFile(location, file, type, maxAge, options = {}) {
113115 } ;
114116}
115117
116- module . exports = servePublicFile ;
118+ // Handles requests to public static files served by Ghost
119+ function servePublicFiles ( siteApp ) {
120+ // Serve sitemap.xsl
121+ siteApp . get ( '/sitemap.xsl' , createPublicFileMiddleware ( 'static' , 'sitemap.xsl' , 'text/xsl' , config . get ( 'caching:sitemapXSL:maxAge' ) ) ) ;
122+
123+ // Serve stylesheets for default templates
124+ siteApp . get ( '/public/ghost.css' , createPublicFileMiddleware ( 'static' , 'public/ghost.css' , 'text/css' , config . get ( 'caching:publicAssets:maxAge' ) ) ) ;
125+ siteApp . get ( '/public/ghost.min.css' , createPublicFileMiddleware ( 'static' , 'public/ghost.min.css' , 'text/css' , config . get ( 'caching:publicAssets:maxAge' ) ) ) ;
126+
127+ // Traffic analytics tracking script
128+ siteApp . get ( '/public/ghost-stats.min.js' , createPublicFileMiddleware ( 'static' , 'public/ghost-stats.min.js' , 'application/javascript' , config . get ( 'caching:publicAssets:maxAge' ) ) ) ;
129+
130+ // Card assets (built on the fly)
131+ siteApp . get ( '/public/cards.min.css' , cardAssets . serveMiddleware ( ) , createPublicFileMiddleware ( 'built' , 'public/cards.min.css' , 'text/css' , config . get ( 'caching:publicAssets:maxAge' ) ) ) ;
132+ siteApp . get ( '/public/cards.min.js' , cardAssets . serveMiddleware ( ) , createPublicFileMiddleware ( 'built' , 'public/cards.min.js' , 'application/javascript' , config . get ( 'caching:publicAssets:maxAge' ) ) ) ;
133+
134+ // Comment counts
135+ siteApp . get ( '/public/comment-counts.min.js' , createPublicFileMiddleware ( 'static' , 'public/comment-counts.min.js' , 'application/javascript' , config . get ( 'caching:publicAssets:maxAge' ) ) ) ;
136+
137+ // Member attribution
138+ siteApp . get ( '/public/member-attribution.min.js' , createPublicFileMiddleware ( 'static' , 'public/member-attribution.min.js' , 'application/javascript' , config . get ( 'caching:publicAssets:maxAge' ) ) ) ;
139+
140+ // Recommendations well-known
141+ siteApp . get ( '/.well-known/recommendations.json' , createPublicFileMiddleware ( 'built' , '.well-known/recommendations.json' , 'application/json' , config . get ( 'caching:publicAssets:maxAge' ) , { disableServerCache : true } ) ) ;
142+
143+ // Serve robots.txt if not found in theme (and blog is not private)
144+ const defaultRobotsTxtMiddleware = createPublicFileMiddleware ( 'static' , 'robots.txt' , 'text/plain' , config . get ( 'caching:robotstxt:maxAge' ) ) ;
145+ siteApp . get ( '/robots.txt' , function serveRobotsTxt ( req , res , next ) {
146+ // If private blogging is enabled, let filterPrivateRoutes handle it
147+ if ( settingsCache . get ( 'is_private' ) ) {
148+ return next ( ) ;
149+ }
150+
151+ const activeTheme = themeEngine . getActive ( ) ;
152+ if ( ! activeTheme ) {
153+ return defaultRobotsTxtMiddleware ( req , res , next ) ;
154+ }
155+
156+ const themeRobotsTxtPath = path . join ( activeTheme . path , 'robots.txt' ) ;
157+ fs . access ( themeRobotsTxtPath , fs . constants . R_OK , ( err ) => {
158+ if ( err ) {
159+ // Theme doesn't have robots.txt, serve default
160+ return defaultRobotsTxtMiddleware ( req , res , next ) ;
161+ }
162+ // Theme has robots.txt, let staticTheme middleware handle it
163+ return next ( ) ;
164+ } ) ;
165+ } ) ;
166+ }
167+
168+ module . exports = servePublicFiles ;
117169module . exports . servePublicFile = servePublicFile ;
118170module . exports . createPublicFileMiddleware = createPublicFileMiddleware ;
0 commit comments