0

I'd like to start by noting that I have already taken the troubleshooting steps to resolve errors 403 and 404 which are commonly associated with this problem in other threads all over stack overflow. These include setting CloudFront error pages to redirect to /index.html on both errors 403 and 404. I have also attempted using a Lambda@Edge function to rewrite the URL.

The issue I'm having is my react page, which uses react router, is being served correctly from aws when I navigate to the root. But when I try to navigate directly to child routes, nothing seems to render. However, when I serve up the same production build on my local machine it's fine.

Here is the public link for reference

Notice if you navigate directly to the link there is no issue. But then if you navigate to https://d1e06h60n3f04n.cloudfront.net/preview/assetId nothing renders. The code for the preview page is as follows:

import React, { useEffect, useState } from 'react';
import { useParams } from 'react-router';
import { BlogPreview } from './BlogPreview';

export interface IBlogPostData {
    title: string;
    content: string;
}

export function BlogPreviewForm() {
    console.log("Loading blog preview form");
    var { postId } = useParams();
    const [blogPostData, setBlogPostData] = useState<IBlogPostData>({ title: "", content: "" });

    useEffect(() => {
        async function loadPost() {
            var fileContent = await fetch(`previews/${postId}`);
            setBlogPostData(await fileContent.json());
        }
        loadPost();
    }, []);

    return (
        <div>
            <div>Blog Preview</div>
            <BlogPreview title={blogPostData?.title} content={blogPostData?.content} />
            <label>Api Key</label>
            <input type="text"></input>
            <button>SUBMIT</button>
        </div>
    );
}

and that page is supposed to fetch the asset from assetId and load it.

My index html:

import ReactDOM from 'react-dom/client';
import './index.css';
import { AppRoutes } from './Routes';
import reportWebVitals from './reportWebVitals';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';

const routes = createBrowserRouter(AppRoutes);

const root = ReactDOM.createRoot(
    document.getElementById('root') as HTMLElement
);

root.render(
    <React.StrictMode>
        <RouterProvider router={routes} />
    </React.StrictMode>
);

reportWebVitals();

and my Routes definition:

export const AppRoutes: RouteObject[] = [
    {
        element: <App />,
        path: '/',
        children: [{
            path: 'preview',
            children: [{
                path: '/preview/:postId',
                element: < BlogPreviewForm />
            }]

        }]
    }
]

I am pretty certain this has to do with CloudFront and S3 since it renders fine locally, so I'm not sure how much posting the relevant code will help which is why I've linked to a URL.

S3 config for reference:

enter image description here

enter image description here

6
  • Check the CRA deployment docs for S3 and CloudFront to get an overview what needs to be configured to correctly serve the React app. Basically none of the frontend code you've shared is responsible, can you edit the post to show the backend hosting settings/configurations? Commented May 22, 2023 at 16:40
  • @DrewReese So, I set up a new s3 bucket and went through the steps provided in the blog post. I figured I'd get that working before bothering with putting CloudFront in the mix. I am having the same issue. What's weird is when I go to test-alfey.s3-website-us-east-1.amazonaws.com/preview and look in the dev console under sources I see the javascript and react components in my static directory. But as soon as I go one level deeper, all of that is gone and it only servers the CSS. I'll edit my post with the S3 config but like I said it should match that blog post exactly now Commented May 22, 2023 at 17:01
  • The basic gist is that the server/hosting service needs to be configured to return the app's root index.html file for all page requests. Commented May 22, 2023 at 17:04
  • @DrewReese understood. The reasoning for it all makes sense, I just don't know why it's not working. I posted edits with my config above Commented May 22, 2023 at 17:19
  • I think this is basically the same problem I'm having, but no solution seems to have ever been posted unfortunately: stackoverflow.com/questions/69282917/… Commented May 22, 2023 at 18:29

1 Answer 1

0

At somepoint in my troubleshooting I had set the homepage variable in my package.json to "."

I was looking in my browser's developer window and noticed I was getting 404 errors on all the files in the static directory located int the root directory of my s3 bucket. This was happening because it was trying to make a path relative to the URL I was loading: e.g. https://d29uq6a9kfzg1q.cloudfront.net/preview/static/js/main.3351ea6b.js

By removing homepage "." from my package.json it switched the paths referencing the static directory to be absolute in my index.html file: e.g. which fixed the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.