ixc / wagtail-instance-selector

A widget for Wagtail's admin that allows you to create and select related items
MIT License
54 stars 17 forks source link

StreamField javascript error for comment on instance selector js in production #18

Closed xeroticikot closed 3 years ago

xeroticikot commented 3 years ago

In my local machine, there are no issues, but in production, I'm getting the following browser errors and the streamfield contents are not loading properly on page editing from admin. Below are the console errors -

Uncaught TypeError: Cannot read property 'call' of undefined
    at n (bootstrap:18)
    at Module.14890 (comments.js?v=a7720a1e:1)
    at n (bootstrap:18)
    at Object.31354 (main.tsx:20)
    at n (bootstrap:18)
    at Object.68251 (comments.js:3)
    at n (bootstrap:18)
    at a (jsonp chunk loading:75)
    at Function.n.x (jsonp chunk loading:88)
    at startup:2
FieldBlock.js:19 TypeError: Cannot read property 'setState' of undefined
    at InstanceSelector.render (instance_selector_telepath.js:16)
    at new t (FieldBlock.js:15)
    at t.render (FieldBlock.js:97)
    at StructBlock.js:53
    at Array.forEach (<anonymous>)
    at new t (StructBlock.js:48)
    at t.render (StructBlock.js:135)
    at e.t (BaseSequenceBlock.js:83)
    at new e (StreamBlock.js:51)
    at e._createChild (StreamBlock.js:261)
t @ FieldBlock.js:19
jquery-3.5.1.min.js?v=a7720a1e:2 jQuery.Deferred exception: window.comments.initCommentsInterface is not a function TypeError: window.comments.initCommentsInterface is not a function
    at HTMLDocument.<anonymous> (http://xeroticinc.codes/admin/pages/3/edit/:120:29)
    at e (http://xeroticinc.codes/static/wagtailadmin/js/vendor/jquery-3.5.1.min.js?v=a7720a1e:2:30005)
    at t (http://xeroticinc.codes/static/wagtailadmin/js/vendor/jquery-3.5.1.min.js?v=a7720a1e:2:30307) undefined
S.Deferred.exceptionHook @ jquery-3.5.1.min.js?v=a7720a1e:2
jquery-3.5.1.min.js?v=a7720a1e:2 Uncaught TypeError: window.comments.initCommentsInterface is not a function
    at HTMLDocument.<anonymous> ((index):120)
    at e (jquery-3.5.1.min.js?v=a7720a1e:2)
    at t (jquery-3.5.1.min.js?v=a7720a1e:2)
FieldBlock.js:46 Uncaught TypeError: window.comments.initAddCommentButton is not a function
    at new t (FieldBlock.js:46)
    at t.render (FieldBlock.js:97)
    at StructBlock.js:53
    at Array.forEach (<anonymous>)
    at new t (StructBlock.js:48)
    at t.render (StructBlock.js:135)
    at e.t (BaseSequenceBlock.js:83)
    at new e (StreamBlock.js:51)
    at e._createChild (StreamBlock.js:261)
    at e.t._insert (BaseSequenceBlock.js:262)
markfinger commented 3 years ago

Given it's only happening in prod environments, I'd assume it's something to do with your configuration for building+serving static assets. Assuming those errors are in sequential order, it looks like Wagtail's comment scripts are failing, which is probably causing a cascade and breaking other things.

Other possibilities...

FieldBlock.js:19 TypeError: Cannot read property 'setState' of undefined ... at InstanceSelector.render (instance_selector_telepath.js:16) refers to https://github.com/ixc/wagtail-instance-selector/blob/ada1dee8381917db89aa12d6a14f3589c892a92f/instance_selector/static/instance_selector/instance_selector_telepath.js#L16. So, something might be breaking in Wagtail's JS 🤷

Otherwise, it could be an overly aggressive JS compressor. window.comments.initCommentsInterface is not a function is a common type of error to see when symbols (method names, variables, etc) are rewritten when the compressor can't find a reference to them.


But, I think the problem's likely to be in your prod env's configuration.

xeroticikot commented 3 years ago

Thank you so much for the thoughtful reply. I actually assumed something like that after upgrading wagtail to 2.13, already opened the same issue on wagtail repo, but they haven't replied any. Also after upgrading to 2.13, only instance selector caused some problems, so I opened an issue here thinking this package may causing the problem. You're absolutely right, I'm using django-compressor for better performance. Here's my compressor config in production -

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
COMPRESS_ENABLED = True
COMPRESS_CSS_HASHING_METHOD = 'content'
COMPRESS_FILTERS = {
    'css':[
        'compressor.filters.css_default.CssAbsoluteFilter',
        'compressor.filters.cssmin.rCSSMinFilter',
    ],
    'js':[
        'compressor.filters.jsmin.JSMinFilter',
    ]
}
HTML_MINIFY = True
KEEP_COMMENTS_ON_MINIFYING = True

Are you suggesting not to use compressors? Thank you so much & I'd really much appreciate any recommendation.

markfinger commented 3 years ago

I've used django-compressor on a lot of projects, but I haven't run into similar problems before. If you turn django-compressor off, does it fix it? If so, you could try incrementally adding the django-compressor options back in until it surfaces the cause. If not, maybe whitenoise is doing something 🤷

Another possibility is that wagtail's doing something for production environments. Eg: if DEBUG=False, they might ship a pre-compressed JS file which is omitting some symbols that are exposed in dev envs.

markfinger commented 3 years ago

I've used django-compressor on a lot of projects, but I haven't run into similar problems before.

To expand on that, I think we've generally used default configuration for django-compressor. So there might be some subtlety in combining wagtail 2.13 and compressor's more aggressive options.

xeroticikot commented 3 years ago

I've rolled back to wagtail 2.12.4 & there are few websites I deployed with this version without any issues such as https://codelinker.net. But after these issues & even after downgrading to 2.12.4, the admin is working fine but wagtailuserbar in frontend is now throwing error when logged in as admin. Below is the stack trace -

Traceback (most recent call last):
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/core/handlers/base.py", line 204, in _get_response
    response = response.render()
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/classytags/core.py", line 151, in render
    return self.render_tag(context, **kwargs)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/sekizai/templatetags/sekizai_tags.py", line 87, in render_tag
    rendered_contents = nodelist.render(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/template/library.py", line 192, in render
    output = self.func(*resolved_args, **resolved_kwargs)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/wagtail/admin/templatetags/wagtailuserbar.py", line 48, in wagtailuserbar
    userprofile = UserProfile.get_for_user(user)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/wagtail/users/models.py", line 64, in get_for_user
    return cls.objects.get_or_create(user=user)[0]
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/models/query.py", line 576, in get_or_create
    return self._create_object_from_params(kwargs, params)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/models/query.py", line 610, in _create_object_from_params
    obj = self.create(**params)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/models/query.py", line 447, in create
    obj.save(force_insert=True, using=self.db)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/models/base.py", line 753, in save
    self.save_base(using=using, force_insert=force_insert,
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/models/base.py", line 790, in save_base
    updated = self._save_table(
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/models/base.py", line 895, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/models/base.py", line 933, in _do_insert
    return manager._insert(
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/models/query.py", line 1254, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1397, in execute_sql
    cursor.execute(sql, params)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/var/www/xeroticinc.codes/xenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: null value in column "updated_comments_notifications" violates not-null constraint
DETAIL:  Failing row contains (11, t, t, t, 1, , , , null).

Without admin login it's just fine. I'm so confused right now!!

markfinger commented 3 years ago

Looks like your DB is out of sync with your codebase. You might need to run some migrations to get it into an appropriate state.

xeroticikot commented 3 years ago

Thank you so much for helping me. The issue solved after clearing the cached static files & rerun "collectstatic" for production. Issue was solved here- https://github.com/wagtail/wagtail/issues/7226