Amplify-js: Error after after hot update: "The schema has already been initialized"

Created on 15 Mar 2020  路  4Comments  路  Source: aws-amplify/amplify-js

Nextjs is doing hot module update when you change the code and you get error.

Problem is here https://github.com/aws-amplify/amplify-js/blob/master/packages/datastore/src/datastore/datastore.ts#L105

Maybe would be better to just return current schema. Also it's probably not the best design decision to execute code during import phase because of unexpected side effects.

SSR feature-request

Most helpful comment

Here is a hack that worked for me:

instead of this:

import { Post } from "src/models" 

Do this:

import { initSchema } from "@aws-amplify/datastore";
import { schema } from "src/models/schema";

let models;
if (typeof window !== "undefined") {
  models = initSchema(schema);
}
// the Post model is now models.Post

This forces initSchema to run only client side.

All 4 comments

+1

Subscribed, thank you @ericclemmons for your work on this, it's so frustrating when working in Next.

Here is a hack that worked for me:

instead of this:

import { Post } from "src/models" 

Do this:

import { initSchema } from "@aws-amplify/datastore";
import { schema } from "src/models/schema";

let models;
if (typeof window !== "undefined") {
  models = initSchema(schema);
}
// the Post model is now models.Post

This forces initSchema to run only client side.

This has been fixed as part of #6146. See this main thread for more info: https://github.com/aws-amplify/amplify-js/issues/5435#issuecomment-692990722

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benevolentprof picture benevolentprof  路  3Comments

DougWoodCDS picture DougWoodCDS  路  3Comments

josoroma picture josoroma  路  3Comments

guanzo picture guanzo  路  3Comments

rygo6 picture rygo6  路  3Comments