Page MenuHomePhabricator

[Bug - development NOT production] Nearby TypeError when using JSONPForeignApi
Closed, ResolvedPublic

Description

nearby.js conditionalizes the options passed to the Nearby constructor. However, it doesn't wait for the Promise to return.

	function refresh( opt ) {
		// check, if the api object (options.api) is already created and set
		if ( options.api === undefined ) {
			// decide, what api module to use to retrieve the pages
			if ( endpoint ) {
// BUG
				loader.using( 'mobile.foreignApi' ).then( function () {
					var JSONPForeignApi = M.require( 'mobile.foreignApi/JSONPForeignApi' );
					options.api = new JSONPForeignApi( endpoint );
				} );
// /BUG
			} else {
				options.api = new mw.Api();
			}
		}
		// make sure, that the api object (if created above) is added to the options object used
		// in the Nearby module
		opt = util.extend( {}, opt, options );

		if ( !nearby ) {
			nearby = new Nearby( opt );
			// todo: use the local emitter when refresh() doesn't recreate the
			//       OO.EventEmitter by calling the super's constructor.
			M.on( NEARBY_EVENT_POST_RENDER, function () {
				var fragment = router.getPath(), el;
				if ( isFragmentIdentifier( fragment ) ) {
					// The hash is expected to be an identifier selector (unless the
					// user entered rubbish).
					el = nearby.$( '#' + fragment );
					if ( el[0] && el[0].nodeType ) {
						$( window ).scrollTop( el.offset().top );
					}
				}
				overlay.hide();
			} );
		}
		nearby.refresh( opt );

Steps to Reproduce

  1. Configure the Nearby endpoint to mirror enwiki: $wgMFNearbyEndpoint = 'https://en.wikipedia.org/w/api.php';.
  2. Visit http://localhost:8080/wiki/Special:Nearby
  3. Tap "show nearby articles."

Expected Results

  • Nearby pages are shown.

Actual Results

  • No pages are shown.
Uncaught TypeError: Cannot read property 'ajax' of undefined
    at NearbyGateway._search (NearbyGateway.js?b33c9:110)
    at NearbyGateway.getPages (NearbyGateway.js?b33c9:60)
    at Nearby._find (Nearby.js?5ed3d:125)
    at Nearby.refresh (Nearby.js?5ed3d:214)
    at refresh (nearby.js?e72fd:95)
    at nearby.js?e72fd:112
    at matchRoute (oojs-router.js?f0f4f:29)
    at Object.<anonymous> (oojs-router.js?f0f4f:94)
    at Function.each (jquery.js?6a07d:360)
    at Router.checkRoute (oojs-router.js?f0f4f:93)

Environments Observed

  • localhost

Browser Version

  • Chromium v69.0.3497.81

OS Version

  • Ubuntu v18.04

Device Model

  • Desktop

Device Language

  • English

Developer notes

This doesn't impact production. JSONPForeignApi is only used for development.
This appears to have been broken in T125820 as the code inside loader.using( 'mobile.foreignApi' ) is dead code in this respect.

loader.using( 'mobile.foreignApi' ).then( function () {
	var JSONPForeignApi = M.require( 'mobile.foreignApi/JSONPForeignApi' );
        // happens after nearby = new Nearby( opt ); is called so has no effect
	options.api = new JSONPForeignApi( endpoint );
} );
nearby = new Nearby( opt );

A fix would make sure this code runs before Nearby is created, or have it moved into Nearby itself.

Details

Related Gerrit Patches:
mediawiki/extensions/MobileFrontend : masterNearby works again locally!

Event Timeline

Restricted Application changed the subtype of this task from "Deadline" to "Task". · View Herald TranscriptSep 19 2018, 8:06 PM
Restricted Application added a subscriber: Aklapper. · View Herald Transcript
Jdlrobson renamed this task from [Bug] Nearby TypeError when using JSONPForeignApi to [Bug - development NOT production] Nearby TypeError when using JSONPForeignApi.Sep 19 2018, 9:36 PM
Jdlrobson updated the task description. (Show Details)
Jdlrobson lowered the priority of this task from Normal to Low.Nov 15 2018, 7:03 PM

Change 476384 had a related patch set uploaded (by Jdlrobson; owner: Jdlrobson):
[mediawiki/extensions/MobileFrontend@master] Nearby works again locally!

https://gerrit.wikimedia.org/r/476384

Change 476384 merged by jenkins-bot:
[mediawiki/extensions/MobileFrontend@master] Nearby works again locally!

https://gerrit.wikimedia.org/r/476384

pmiazga claimed this task.Dec 3 2018, 6:03 PM
pmiazga added a subscriber: Jdlrobson.

Works as expected

pmiazga closed this task as Resolved.Dec 3 2018, 10:52 PM
pmiazga removed pmiazga as the assignee of this task.
pmiazga removed a project: Patch-For-Review.
pmiazga added a subscriber: pmiazga.