Method get of Storage is not supported for SSR
See original GitHub issueDescribe the bug I’m trying to get an image using the method get from Storage, like this:
export const getServerSideProps: GetServerSideProps = async ({
params,
req,
}) => {
const { id } = params;
const SSR = withSSRContext({ req });
try {
const { data } = await (SSR.API.graphql({
query: getBasicUserInformation,
variables: {
id,
},
}) as Promise<{ data: GetUserQuery }>);
if (data.getUser.id) {
const picture = data.getUser.picture
// the method get does not exist
? ((await SSR.Storage.get(data.getUser.picture)) as string)
: null;
return {
props: { ...data.getUser, picture },
};
}
} catch (error) {
console.error(error);
return {
props: {
id: null,
},
};
}
};
This is the error I get:
TypeError: Cannot read property 'get' of null
at getServerSideProps (webpack-internal:///./src/pages/perfil/[id]/index.tsx:71:64)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async renderToHTML (/home/andres/Entrepreneurship/TeVi/node_modules/next/dist/next-server/server/render.js:39:215)
at async /home/andres/Entrepreneurship/TeVi/node_modules/next/dist/next-server/server/next-server.js:99:97
at async /home/andres/Entrepreneurship/TeVi/node_modules/next/dist/next-server/server/next-server.js:92:142
at async DevServer.renderToHTMLWithComponents (/home/andres/Entrepreneurship/TeVi/node_modules/next/dist/next-server/server/next-server.js:124:387)
at async DevServer.renderToHTML (/home/andres/Entrepreneurship/TeVi/node_modules/next/dist/next-server/server/next-server.js:125:874)
at async DevServer.renderToHTML (/home/andres/Entrepreneurship/TeVi/node_modules/next/dist/server/next-dev-server.js:34:578)
at async DevServer.render (/home/andres/Entrepreneurship/TeVi/node_modules/next/dist/next-server/server/next-server.js:72:236)
at async Object.fn (/home/andres/Entrepreneurship/TeVi/node_modules/next/dist/next-server/server/next-server.js:56:618)
at async Router.execute (/home/andres/Entrepreneurship/TeVi/node_modules/next/dist/next-server/server/router.js:23:67)
at async DevServer.run (/home/andres/Entrepreneurship/TeVi/node_modules/next/dist/next-server/server/next-server.js:66:1042)
at async DevServer.handleRequest (/home/andres/Entrepreneurship/TeVi/node_modules/next/dist/next-server/server/next-server.js:34:1081)
Expected behavior Get the image from the getServerSideProps function
- Device: Ubuntu 20
- Browser Google chrome
- Versions:
"aws-amplify": "^3.3.13",
"next": "^10.0.4",
"react": "^17.0.1",
"react-dom": "^17.0.1",
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Next.js use localstorage problem with SSR - Stack Overflow
React Hook useEffect has missing dependencies: 'initialize' and 'key'. So when i add initialzie method to the depenedncy, it says that the ...
Read more >SSR Support for AWS Amplify JavaScript Libraries
Enabling Server-Side Rendering (SSR) support in an Amplify app ... how to build SSR Next.js apps using Amplify using the new Next.js getting...
Read more >Incremental Static Regeneration - Data Fetching - Next.js
Note: The experimental-edge runtime is currently not compatible with ISR, although can leverage stale-while-revalidate by setting the cache-control header ...
Read more >Getting Started with Server-Side Rendering (SSR) - JavaScript
If your client-side code only reads from the server-side props and doesn't perform any updates to these models, then your client-side code won't...
Read more >Using localStorage with React Hooks - LogRocket Blog
Working with a fresh React application, let's head over to the ... To see a list of the Storage methods, open the browser...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There’s good news & bad news to this.
The good news is that you can provide
modules
towithSSRContext
to get new instances of categories per-request:The bad news is that the
Storage
category usesAWSS3Provider
, which does not have access to request-specific credentials.Note to self – proposal on how to fix this
The proposed changes would make Storage such that:
Credentials
defaults to the single instance shared on the client (like it does today).Credentials
.withSSRContext
is called, it creates a new instance ofStorage
&Credentials
, and a new instance ofAWSS3Provider
as well.Credentials
will be populated with any cookie-based credentials from the client, and passed through the chain toAWSS3Provider
.This means there’s more work for us to do to add proper
Storage
support for SSR:AWSS3Provider
uses a single instance ofCredentials
, but should use a scoped instance (e.g.this.Credentials
)https://github.com/aws-amplify/amplify-js/blob/e0789af92ad043bd4c069d766158942a5e6b2cae/packages/storage/src/providers/AWSS3Provider.ts#L455-L457
Even with
this.Credentials
used withinAWSS3Provider
, they still need to be injected.Storage
first needs to declareCredentials
as an instance variable.Other categories like
Auth
haveCredentials
injected into them because they declare an instance property:https://github.com/aws-amplify/amplify-js/blob/e0789af92ad043bd4c069d766158942a5e6b2cae/packages/auth/src/Auth.ts#L91-L101
AWSS3Provider
is injected by default when no other pluggables are defined:https://github.com/aws-amplify/amplify-js/blob/e0789af92ad043bd4c069d766158942a5e6b2cae/packages/storage/src/Storage.ts#L155-L157
When pluggables are configured, they can use also pass
{ Credentials = this.Credentials }
:https://github.com/aws-amplify/amplify-js/blob/e0789af92ad043bd4c069d766158942a5e6b2cae/packages/storage/src/Storage.ts#L64
Finally,
AWSS3Provider
can overridethis.Credentials
based on the value inconfig
:https://github.com/aws-amplify/amplify-js/blob/e0789af92ad043bd4c069d766158942a5e6b2cae/packages/storage/src/providers/AWSS3Provider.ts#L106-L115
Thanks! If someone wants to do something similar, one quick and simple way is to just use
Credentials
from@aws-amplify/core
and get the image using theaws-sdk
, something like this (In my case I’m using the api from next.js/vercel):That’s it, works fine for me, while this is resolved by the Amplify team 😃.