From 58db0352e6359d4b1c69391d260105abbe4ede60 Mon Sep 17 00:00:00 2001 From: Rajarshee Chatterjee Date: Sat, 25 Jul 2026 08:55:23 +0530 Subject: [PATCH] fix: Restore Metro Reader Asset Middleware --- metro.config.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/metro.config.js b/metro.config.js index 821cb86e..90af64db 100644 --- a/metro.config.js +++ b/metro.config.js @@ -1,7 +1,58 @@ const { getDefaultConfig } = require('expo/metro-config'); +const fs = require('fs'); +const path = require('path'); const config = getDefaultConfig(__dirname); +const readerAssetsRoot = path.resolve(__dirname, 'assets', 'reader'); +const readerAssetContentTypes = { + '.css': 'text/css; charset=utf-8', + '.js': 'text/javascript; charset=utf-8', + '.ttf': 'font/ttf', +}; config.resolver.sourceExts.push('sql'); +config.server.enhanceMiddleware = metroMiddleware => { + return (request, response, next) => { + let pathname; + + try { + pathname = decodeURIComponent( + new URL(request.url, 'http://localhost').pathname, + ); + } catch { + return metroMiddleware(request, response, next); + } + + if (!pathname.startsWith('/assets/')) { + return metroMiddleware(request, response, next); + } + + const assetPath = path.resolve( + readerAssetsRoot, + pathname.slice('/assets/'.length), + ); + const isReaderAsset = + assetPath.startsWith(`${readerAssetsRoot}${path.sep}`) && + fs.existsSync(assetPath) && + fs.statSync(assetPath).isFile(); + + if (!isReaderAsset) { + return metroMiddleware(request, response, next); + } + + response.setHeader( + 'Content-Type', + readerAssetContentTypes[path.extname(assetPath)] || + 'application/octet-stream', + ); + + if (request.method === 'HEAD') { + return response.end(); + } + + fs.createReadStream(assetPath).pipe(response); + }; +}; + module.exports = config;