sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

SPFx web part on classic web sites causes uncaught type error

Open MarcoSimon opened this issue 9 years ago • 30 comments

Category

  • [x] Bug

Expected or Desired Behavior

The web part should load properly on classic publishing pages (in classic web sites) as it does on modern pages (the development environment).

Observed Behavior

When I try to implement a SPFx web part including React, Redux and some other modules (namely moment, immutable, jQuery and superagent), written in TypeScript, I'm getting an 'Uncaught TypeError: Unexpected anonymous AMD define.': typeerror_branding

The error occurs at the point define(factory) in 'branding.o365.js' where moment.js should be included (line 10263): typeerror_moment

Steps to Reproduce

The app has been developed and successfully tested on a modern page and wiki page in a modern team site. Even when it was deployed on the production tenant, it worked on these page types. But when the web part is placed on a classic wiki page or publishing page (in a classic team site), we experience the described behavior.

If you need some more detailed information, feel free to ask for it! Tags: #spfx

MarcoSimon avatar Jul 10 '17 09:07 MarcoSimon

Thanks for reporting this issue. Could you include the sppkg file that you are using which causes this error? It would help us investigate the issue.

GrahamMcMynn avatar Jul 11 '17 21:07 GrahamMcMynn

Thanks for your answer. Please find the zipped sppkg file attached. I hope it can help to figure out what the issue is. webpart-beezy-activities.zip

MarcoSimon avatar Jul 12 '17 06:07 MarcoSimon

Hello Marco,

I was not able to find branding.o365.js in the package, so I'm wondering how you're loading it on the page. Is it coming from a master page? If so, is it using requirejs to load modules?

moyali avatar Jul 20 '17 20:07 moyali

Thanks for your reference to the branding.o365.js, I just didn't recognize it before! Now I could have a look at the branding wich is loaded via 'Custom User Action'.

I figured out the following:

  1. When using our Beezy webpart on an un-branded site collection (no branding.o365.js is loaded), or using the branded site collection without the Beezy webpart, everything works without any error.
  2. We don't use require.js, but some included modules are defined as anonymous AMD modules (moment.js, magnific-popup, jQuery and some jQuery UI widgets). When I don't include them, it works.
  3. When I enable browser caching, the webpart works (mostly) as expected, even with AMD modules. But when disabling the cache, the loading procedure crashes in sp-classic-page-assembly…js, although the loading order of the scripts obviously remains the same (I can post some screenshots if needed). See the different error message in the screenshot attached: multiple_anonymous_defines

MarcoSimon avatar Jul 27 '17 15:07 MarcoSimon

How do you load the anonymous AMD modules? From the callstack, it looks like it may be using the global define() that SPFx sets for loading modules as a script. The problem is not on loading the JS files, but eval'ing them. Understanding what's your use case when loading those scripts will help understand how adding SPFx in the page impacts with that.

mpasarin avatar Jul 28 '17 19:07 mpasarin

The AMD modules (moment.js, jQuery, jQueryUI) are concatenated by a gulp action in the branding.o365.js which is injected into the -tag of the DOM by an UserCustomAction-ScriptLink, so the modules should be in global context. These are used for the branding tasks in the classic page. We don't use require.js explicitly here, but we noticed that require.js is used in the SharePoint page context itself. So it may be that this require.js instance influences our scripts. In the modern webpart, the modules (moment.js, React, Redux, ...) are required by 'import'-statement and bundled by webpack as usual.

MarcoSimon avatar Jul 31 '17 14:07 MarcoSimon

We are in the process in rolling out a change in the loader using RequireJS that (as a side effect) will probably fix that problem directly. If you want, contact me at [email protected] and we can sync up and check that it will fix your problem (or otherwise debug it together). As it's something specific with your classic experience, I think it'll be more efficient to do it this way.

mpasarin avatar Jul 31 '17 17:07 mpasarin

Please repoen this issue if you don't see a fix within 3 weeks

ghost avatar Aug 07 '17 18:08 ghost

Hi,

We are also experiencing this issue. We have a self calling JavaScript method that loads some analytics (a but like Google Analytics does) that we run via a Site scoped UserCustomAction. This has worked for years on classic pages, but, by design, doesn't work on modern pages.

Now, when we put a SPFx Client Side Web Part on a classic wiki page, we get the exact error message shared by the OP.

By the nature of @mpasarin's reply, can I assume that we should see this start to work with the rollout of the change eluded to on the 31 Jul? Do we know when we are likely to see that in our tenants so that we can update our customers?

Regards,

MarkStokes-outlook avatar Oct 03 '17 14:10 MarkStokes-outlook

We too are still seeing the same issue. We have a very simple SPFx web part which, when added to a classic SharePoint page, produces the error "Unexpected anonymous AMD define".

It would be good to know whether the change eluded to previously is likely to have been rolled out completely or if there is further debugging and fixing that we must do.

JHolah avatar Oct 05 '17 14:10 JHolah

We need this bug resolving very quickly as it is impacting a number of customer deployments.

@mpasarin, do you have a timeline that we can give to our customers of when this will be resolved?

MarkStokes-outlook avatar Oct 11 '17 08:10 MarkStokes-outlook

The change rollout had to be stopped due to unforeseen side effects. After fixing those, we are currently rolling out those changes. The expectation should be that in 2-3 weeks the issue should be resolved.

mpasarin avatar Oct 11 '17 17:10 mpasarin

We worked around this issue by not loading our script dependencies in the page via javascript. We packaged everything up into one file and loaded this directly via custom action. The problem arises when script dependencies are loaded "later" asynchronously when SharePoints require.js is active. This seems to happen only when there is an modern webpart on the page. But this approach won't work for all cases.

Regards Tobias

toblum avatar Oct 16 '17 16:10 toblum

Hello everyone,

Any news about this issue ?

EDIT : I worked around the issue. I set the "define" property to undefined before the script injection, after the script loaded I redefine the "define" property.

Regards

Ibrahim

Supaibo avatar Dec 07 '17 10:12 Supaibo

I haven’t implemented the workaround suggested by Supaibo yet but we are still experiencing this issue. Was anyone else able to solve this without the workaround? Are other people still experiencing this as well?

@mpasarin: Can you confirm that the changes to solve this issue are rolled out?

Regards, Rob

RobLempens avatar Mar 02 '18 08:03 RobLempens

Hi has there been any updates for this? I'm using the pnp javascript loader to load javascript files for branding, analytics etc and get this error with a classic 2016 SP On-premises page with a SPFx webpart.

sonicbaz avatar May 29 '18 22:05 sonicbaz

@sonicbaz Having same issues with the AMD conflict.

ravinleague avatar Jul 08 '18 22:07 ravinleague

@mpasarin any hope of getting a fix pushes to SharePoint 2016 on-prem?

samculver avatar Feb 14 '19 23:02 samculver

Can anyone confirm this is still an issue with SharePoint Online as this has been open for quite some time... likely fixed in SPO...

@samculver Unfortunately there's no story for updating SPFx in on-prem environments.

andrewconnell avatar Feb 20 '19 20:02 andrewconnell

SPFx webparts load properly in our default template SPO classic pages.

We have a client though who has a customized template in SPO. They are now encountering this issue in their custom template but not in SPOs classic default template.

Is it possible for the DEV to shed some light on how this was fixed? We'd like to pass this information onto our client.

butchmarshall avatar Feb 21 '19 14:02 butchmarshall