Front-End Web & Mobile
Add storage to a Next.js 13 app with AWS Amplify
This post builds on the initial post, Deploy a Next.js 13 app with authentication to AWS Amplify, which initialized our project with AWS Cognito authentication and deployed our project to Amplify Hosting and the previous post, Build a Product Roadmap with Next.js and Amplify, where we built an admin page for product managers to login and update the roadmap.
Display the Roadmap to Customers
Our requirements state that the customer facing roadmap be performant and up-to-date within an hour of publishing a new feature to our roadmap. The Incremental Static Regeneration (ISR) feature from Next.js allows you to create or update static pages after you’ve built your site.
Update pages/index.js
with the following code to show customers the features entered via the roadmap admin.
Per our requirements, this page is implemented as ISR and using a filter retrieves the released features only in getStaticProps
, then displays them in a list. During the build phase a static page is generated. The revalidate
prop, currently set to 1 hour, sets a cache for the page based on its value. A visitor to the page after the hour will cause the page to be regenerated and cached, and will show the latest updates made by the product managers.
Visit http://localhost:3000
to view the page.
Attach internal documents to roadmap items
A final requirement is to allow product managers to save an internal document with a roadmap item.
Using the Amplify CLI, we need to enable Storage for the documents.
From the root of the project run:
amplify add storage
And follow the prompts
Finally run amplify push
to deploy the Storage resources.
Next, we need to update our FeatureForm in the AmpliCar Admin page to allow for a file to be uploaded.
First we import Storage
from aws-amplify
. Next, we need to track the state of our internal document via the internalDoc
state variable, which we add to our useEffect
, resetFormFields
and handleSaveFeature
.
handleUploadDoc
will upload our document using the Storage
library and handleRemoveDoc
will remove the document if the user clicks on the button to delete it. Finally, we display a file input or list the filename if a product manager has attached a document.
Next, we need to update our FeaturesTable
component to provide a way for other product managers to download the file and delete the file in storage at the same time it is deleting the feature from the API.
First, we import Storage
from aws-amplify
. Next, we construct handleDownload
which retrieves the file from Storage
and makes it downloadable using the downloadBlob
function which uses the Blob API to programmatically download Blobs using JavaScript. The onDeleteInternalDoc
function is constructed to delete the file using the Storage API.
Then, we update the Delete button to implement a Promise.all
calling both onDeleteInternalDoc
and handleDeleteFeature
.
Finally, we add our Download File button which calls handleDownload when clicked.
With this our application is complete!
What we’ve built
Referring to our requirements, we wanted an application that allowed product managers to create and maintain a public roadmap for customers. For the public roadmap page, we added ISR to update the page at an interval appropriate to our use case and benefit from the efficiency of caching. The roadmap admin needed to feel like an application, updating the UI with every edit or delete, so subscriptions were used to offer real-time feedback to product managers. Finally, we added the capabilities to attach an internal document to a feature.