OMGImOut
OMGImOut

Reputation: 182

How to make iOS universal links working with Vite + CRA. Vite seems to redirect url when apple-app-site-association file when it isn't .json

I'm using CRA which was migrated to Vite. Im uploading apple-app-site-association file (without .json extension) by placing it in public folder. Vite is building this file because it can read it later if I add .json extension. So can can read it like https://.com/apple-app-site-association.json

But according to apple docs: "Don’t append .json to the apple-app-site-association filename." and when I save Don’t append .json to the apple-app-site-association file and build the app, the url https://.com/apple-app-site-association redirecting to "/". Any idea what I did wrong?

Upvotes: 2

Views: 295

Answers (1)

user14938230
user14938230

Reputation: 1

I had the same problem (vite at 5.1.2)

in your vite.config.ts you can add this:

plugins:
[react(),
    {
        name: 'configure-server',
        configureServer(server) {
            server.middlewares.use((req, res, next) => {
                console.log('\n request url original url. ', req.originalUrl)
                if (req.originalUrl === '/.well-known/apple-app-site-association') {
                    res.setHeader('Content-Type', 'application/json');
                    const filePath = path.join(__dirname, 'public/.well-known/apple-app-site-association');
                    fs.createReadStream(filePath).pipe(res);
                } else {
                    next();
                }
            });
        },
        configurePreviewServer(server) {
            server.middlewares.use((req, res, next) => {
                console.log('\n request url original url. ', req.originalUrl)

                if (req.originalUrl === '/.well-known/apple-app-site-association') {
                    res.setHeader('Content-Type', 'application/json');
                    const filePath = path.join(__dirname, 'public/.well-known/apple-app-site-association');
                    fs.createReadStream(filePath).pipe(res);
                } else {
                    next();
                }
            });
        }
    }
],

Upvotes: 0

Related Questions